我使用Asp.Net MVC實體框架。我有一個如下所示的表單。在Asp.Net中保存多對多關係表MVC
這裏,下拉列表是從一個表(類型)填充。複選框從另一個表格(測試)填充。表格如下:
public class Types
{
public int TypesID{get;set;}
public string TestName { get; set; }
public string TestExplanation { get; set; }
public int TestTime { get; set; }
}
public class Tests
{
public int TestID{get;set;
public string Name { get; set; }
public string Code { get; set; }
}
public class Types_Tests
{
public int Types_TestsID{ get; set; }
public int TypesID { get; set; }
public int TestsID { get; set; }
public virtual Types Types { get; set; }
public virtual Tests Tests { get; set; }
}
Types_test表是類型和測試之間的關係表。當我點擊Kaydet按鈕時,它會保存類型和檢查測試。我使用ViewBag,javascript和hdnvalue.I添加了checkboz值到hdntext。我做了如下的保存過程:
[HttpPost]
public ActionResult Index(string drpType, string hdntesttypes)
{
var TypeList = Types.GetAll();
ViewBag.TypesList = new SelectList(TypeList, "Id", "Name");
var testypeList = testTypes.GetAll();
ViewBag.TestTypesList = new SelectList(testypeList, "Id", "TestName");
GenericRepository<TestDisabledTypes> testDisabledRepository = new GenericRepository<TestDisabledTypes>(_context);
if (!string.IsNullOrEmpty(hdntesttypes))
{
string[] disabletypesArray = hdntesttypes.Split(',');
using (TransactionScope trns = new TransactionScope())
{
for (int i = 0; i < disabletypesArray.Length; i++)
{
Test_Types types = new Test_Types();
types.TestTypesID = Convert.ToInt32(disabletypesArray[i]);
types.TypesID = Convert.ToInt32(drpType);
testDisabledRepository.Insert(types);
}
trns.Complete();
}
}
return View();
}
它是wokrs。但是我爲這個過程尋找更好的解決方案。有人可以給我任何想法嗎? 謝謝。
我怎麼能採取速滑運動員的肌肉在這種情況下,表的關係。 – miyamotomusashi
你不需要,你使用對象,不直接與關係一起工作。這是EF的做法 –