public ActionResult AddGroup(Web_UserGroup model) { ViewBag.ALLFunctions = new Web_Sys_FunctionBLL().GetAllFunctions(dbParm); try { string t = Request["FunctionId"].ToString();//获取选中的分类ID格式为("1,2,3”)不包含括号。 List<Int32> idlist = new List<int>(); if (!string.IsNullOrEmpty(t)) { if (t.Contains(",")) { foreach (var each in t.Split(',').ToList()) { idlist.Add(Convert.ToInt32(each)); } } else { idlist.Add(Convert.ToInt32(t)); } } IEnumerable<Web_Sys_Function> selectedFuncs = new Web_Sys_FunctionBLL().GetAllFunctions(dbParm).Where(p => idlist.Contains(p.ID)); if (model.Group_ID == 0) //New { model.Web_Sys_Functions = selectedFuncs.ToList(); //model.Web_Sys_Functions = new List<Web_Sys_Function>(); //selectedFuncs.ToList().ForEach(f => model.Web_Sys_Functions.Add(f)); groupBLL.AddNew(model, dbParm); } else //Edit { List<Web_Sys_Function> originalFuncs = groupBLL.Get(model.Group_ID, dbParm).Web_Sys_Functions; model.Web_Sys_Functions = originalFuncs; //要删除的function var dlist = originalFuncs.Where(p => !selectedFuncs.Contains(p)).ToList(); //要添加的function var alist = selectedFuncs.Where(p => !originalFuncs.Contains(p)).ToList(); foreach (var item in dlist) { model.Web_Sys_Functions.Remove(item); } foreach (var item in alist) { model.Web_Sys_Functions.Add(item); } groupBLL.Update(model, dbParm); } ViewBag.ALLFunctions = new Web_Sys_FunctionBLL().GetAllFunctions(dbParm); return null; //return View(); } catch (Exception ex) { var y = ex.InnerException; return View(model); } }