无法转换类型为'System.Data.Entity.DynamicProxies.QuanTri_8853081F254D1D30AC78D5C548A68F29C932951DF7E094DA751FE0A3AA80E911'的对象

时间:2018-08-21 13:59:05

标签: asp.net asp.net-mvc

        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有问题。希望每个人都帮助我

0 个答案:

没有答案