public ActionResult AddChucNangQuanTri()
{
ViewBag.MaQuanTri = new SelectList(db.QuanTris, "ID", "Ten");
var data = db.ChucNangs.ToList();
Chuc_Nang_Quan_Tri cnqt = new Chuc_Nang_Quan_Tri();
cnqt.ChucNang = data;
return View(cnqt);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddChucNangQuanTri(FormCollection fc)
{
if (ModelState.IsValid)
{
var a = fc.GetValues("MaChucNang");
int quantri = Convert.ToInt32(fc["MaQuanTri"].ToString());
List<Chuc_Nang_Quan_Tri> del = db.Chuc_Nang_Quan_Tri.Where(x=>x.MaQuanTri == quantri).ToList();
foreach (var item in del)
{
db.Chuc_Nang_Quan_Tri.Remove(item);
}
db.SaveChanges();
Chuc_Nang_Quan_Tri cnqt = new Chuc_Nang_Quan_Tri();
for (int i = 0; i < a.Count(); i++)
{
cnqt.MaQuanTri = quantri;
cnqt.MaChucNang = int.Parse(a[i]);
db.Chuc_Nang_Quan_Tri.Add(cnqt);
db.SaveChanges();
}
ViewBag.MaQuanTri = new SelectList(db.QuanTris, "ID", "Ten", quantri);
return View();
}
return View();
}
enter image description here #### view
我需要查看2个列表。一个是SelectList,另一个是复选框列表,我保存了它,但是遇到了这个问题 我认为viewbag.MaQuanTri有问题。希望每个人都帮助我