public void SaveMenu(Menu menu) { using (DeepBlueEntities context = new DeepBlueEntities()) { if (menu.MenuID == 0) { context.Menus.AddObject(menu); } else { // Define an ObjectStateEntry and EntityKey for the current object. EntityKey key = default(EntityKey); object originalItem = null; key = context.CreateEntityKey("Menus", menu); // Get the original item based on the entity key from the context // or from the database. if (context.TryGetObjectByKey(key, out originalItem)) { // Call the ApplyCurrentValues method to apply changes // from the updated item to the original version. context.ApplyCurrentValues(key.EntitySetName, menu); } } context.SaveChanges(); } }
private IEnumerable<ErrorInfo> Validate(Menu menu) { return ValidationHelper.Validate(menu); }
public ActionResult UpdateMenu(FormCollection collection) { ICacheManager cacheManager=new MemoryCacheManager(); EditMenuModel model=new EditMenuModel(); ResultModel resultModel=new ResultModel(); this.TryUpdateModel(model); if(ModelState.IsValid) { Menu menu=AdminRepository.FindMenu(model.MenuID); if(menu==null) { menu=new Menu(); } menu.DisplayName=model.DisplayName; menu.ParentMenuID=model.ParentMenuID; menu.URL=model.URL; menu.Title=model.Title; IEnumerable<ErrorInfo> errorInfo=AdminRepository.SaveMenu(menu); if(errorInfo!=null) { resultModel.Result+=ValidationHelper.GetErrorInfo(errorInfo); } else { // Remove entity menu cache cacheManager.RemoveByPattern(MenuHelper.ENTITYMENUKEY); resultModel.Result="True||"+menu.MenuID; } } else { foreach(var values in ModelState.Values.ToList()) { foreach(var err in values.Errors.ToList()) { if(string.IsNullOrEmpty(err.ErrorMessage)==false) { resultModel.Result+=err.ErrorMessage+"\n"; } } } } return View("Result",resultModel); }