public ActionResult Create(RolesViewModel model) { var permissions = GetGroupForeignData(); ViewBag.Data_Permissions = permissions; ServiceResult result = new ServiceResult(); TempData["Service_Result"] = result; if (ModelState.IsValid) { try { Roles entity = new Roles(); entity.Name = model.Name; entity.Description = model.Description; var permissionsArray = model.Permissions.Split(',').Select(x => Convert.ToInt32(x)).ToList(); var PermissionList = PermissionsService.GetALL().Where(x => permissionsArray.Contains(x.ID)); entity.Permissions.AddRange(PermissionList); RolesService.Create(entity); result.Message = "添加角色成功!"; LogHelper.WriteLog("添加角色成功"); return RedirectToAction("index"); } catch (DbEntityValidationException ex) { result.Message = Utilities.GetInnerMostException(ex); result.AddServiceError(result.Message); LogHelper.WriteLog("添加角色错误", ex); return View(model); } } else { result.Message = "请检查表单是否填写完整!"; result.AddServiceError("请检查表单是否填写完整!"); return View(model); } }
public ActionResult Edit(int ID) { RolesViewModel model = new RolesViewModel(); var entity = RolesService.GetALL().Include(x => x.Permissions).Single(x => x.ID == ID); model.Name = entity.Name; model.ID = entity.ID; model.Description = entity.Description; List<int> PermissionsList = new List<int>(); PermissionsList = entity.Permissions.Select(x => x.ID).ToList(); var permissions = GetGroupForeignData(PermissionsList); ViewBag.Data_Permissions = permissions; return View(model); }