コード例 #1
0
        public ActionResult Edit(Guid id, SecurityRoleModel model, string button)
        {
            using (DBEntities context = Settings.CreateDataContext())
            {
                Validate(context, model);

                if (!ModelState.IsValid)
                {
                    return View(model);
                }

                SecurityRole target = null;
                if (model.Id != Guid.Empty)
                {
                    target = SecurityRoleHelper.Get(model.Id, context);
                    if (target == null)
                    {
                        ModelState.AddModelError("", Resources.Resource.RowNotFound);
                        return View(model);
                    }
                }
                else
                {
                    target = new SecurityRole();
                    target.Id = Guid.NewGuid();
                    context.AddToSecurityRole(target);
                }

                SecurityRoleModel.CreateMap();
                Mapper.Map(model, target);
                UpdateArray(context, model, target);

                try
                {
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    var sb = new StringBuilder(Resources.Resource.SaveError + ": " + ex.Message);
                    if (ex.InnerException != null)
                        sb.AppendLine(ex.InnerException.Message);
                    ModelState.AddModelError("", sb.ToString());
                    return View(model);
                }

                if (button == "SaveAndExit")
                    return RedirectToAction("Index");
                else
                    return RedirectToAction("Edit", new { target.Id });
            }
        }
コード例 #2
0
 private void Validate(DBEntities context, SecurityRoleModel model)
 {
     //string res = SecurityRoleValidator.CheckTableName(context, model.Id, model.TableName);
     //if (res.Length > 0)
     //    ModelState.AddModelError("TableName", res);
 }
コード例 #3
0
        private void UpdateArray(DBEntities context, SecurityRoleModel model, SecurityRole target)
        {
            model.Groups = model.Groups ?? new List<Guid>();
            foreach (SecurityGroup item in target.SecurityGroup.ToList())
            {
                if (!model.Groups.Contains(item.Id))
                    target.SecurityGroup.Remove(item);
            }

            foreach (SecurityGroup item in context.SecurityGroup.Where(
                s => model.Groups.Contains(s.Id)).ToList())
            {
                if (!target.SecurityGroup.Any(any => item.Id == any.Id))
                    target.SecurityGroup.Add(item);
            }
        }