|
本帖最后由 kingdawn2015 于 2017-5-14 17:55 编辑
闲来无事,把代码重新整理了一下,原来的解决办法只是权宜之计,不算得规范,下面的代码看起来应该要清楚的多: public ActionResult MenuEdit_btnSaveClose_Click([Bind(Include = "ID,Name,ImageUrl,NavigateUrl,Remark,SortIndex,ParentID")]AppBoxMvc.Models.Menu menu, string ViewPowerName)
{
if (ModelState.IsValid)
{
AppBoxMvc.Models.Menu item = db.Menus
.Include(m => m.Parent).Include(m => m.ViewPower)
.Where(m => m.ID == menu.ID).FirstOrDefault();
item.Name = menu.Name;
item.ImageUrl = menu.ImageUrl;
item.NavigateUrl = menu.NavigateUrl;
item.SortIndex = menu.SortIndex;
item.Remark = menu.Remark;
int parentID = menu.ParentID.Value;
if (menu.ParentID == -1)
{
item.Parent = null;
}
else
{
AppBoxMvc.Models.Menu pmenu = Attach<AppBoxMvc.Models.Menu>(parentID);
item.Parent = pmenu;
}
if (String.IsNullOrEmpty(ViewPowerName))
{
item.ViewPower = null;
}
else
{
item.ViewPower = db.Powers.Where(p => p.Name == ViewPowerName).FirstOrDefault();
}
db.SaveChanges();
// 关闭本窗体(触发窗体的关闭事件)
ActiveWindow.HidePostBack();
}
return UIHelper.Result();
}
|
|