public ActionResult Edit(int?ID, int?vtype, FormCollection collection, CMSPage model) { var page = new CMSPage() { LastMod = DateTime.Now, }; bool exist = false; bool isNew = false; if (model.PageName.IsNullOrEmpty()) { ModelState.AddModelError("", "Необходимо указать название страницы"); return(PartialView(model)); } if (model.URL.IsNullOrEmpty()) { ModelState.AddModelError("", "Необходимо указать URL страницы"); return(PartialView(model)); } if (ID.HasValue && ID > 0) { ViewBag.Header = "Редактирование раздела"; page = db.CMSPages.FirstOrDefault(x => x.ID == ID); exist = true; if (page == null) { ModelState.AddModelError("", "Страница не найдена"); return(PartialView(model)); } } else { isNew = true; ViewBag.Header = "Создание нового раздела"; page.OrderNum = (db.CMSPages.Count() + 1) * 10; db.CMSPages.InsertOnSubmit(page); } var oldParent = page.ParentID; page.LoadPossibleProperties(model, new[] { "ID", "UrlPath", "BreadCrumbs", "LinkedBreadCrumbs", "ParentID" }); if (page.FullName.IsNullOrEmpty()) { page.FullName = ""; } if (page.FullNameH2.IsNullOrEmpty()) { page.FullNameH2 = ""; } if (page.Title.IsNullOrEmpty()) { page.Title = ""; } if (page.Keywords.IsNullOrEmpty()) { page.Keywords = ""; } if (page.Description.IsNullOrEmpty()) { page.Description = ""; } page.LastMod = DateTime.Now; var parents = CMSPage.FullPageTable.Where(x => x.ID != ID).ToList(); parents.Insert(0, new CMSPage() { ID = 0, PageName = "Корневой раздел сайта" }); ViewBag.Parents = new SelectList(parents, "ID", "PageName", page.ParentID ?? 0); ViewBag.Types = new SelectList(db.PageTypes.Where(x => x.Enabled).OrderBy(x => x.Ordernum).AsEnumerable(), "ID", "Description"); if (!vtype.HasValue) { vtype = 1; } if (model.ParentID == 0 || !model.ParentID.HasValue) { page.ParentID = null; } else { page.ParentID = model.ParentID; } if (page.ParentID == page.ID) { page.ParentID = oldParent; } if (page.ParentID == page.ID) { page.ParentID = null; } ViewBag.VType = vtype; try { db.SubmitChanges(); page.SaveLangValues(); /* * * if (model.ParentID == 0 || !model.ParentID.HasValue) * page.ParentID = null; * else * page.Parent = db.CMSPages.FirstOrDefault(x => x.ID == model.ParentID); * * db.SubmitChanges(); */ ModelState.AddModelError("", "Данные сохранены"); } catch (Exception ex) { ModelState.AddModelError("", ex.Message); } var roles = collection.AllKeys.Where(x => x.StartsWith("ID_")) .Select( x => new { Value = (bool)collection.GetValue(x).ConvertTo(typeof(bool)), ID = new Guid(x.Split <string>("_").ToArray()[1]) }); foreach (var role in roles) { var rel = db.CMSPageRoleRels.FirstOrDefault( x => (role.ID == new Guid() ? !x.RoleID.HasValue : x.RoleID == role.ID) && x.PageID == page.ID); if (role.Value && rel == null) { db.CMSPageRoleRels.InsertOnSubmit(new CMSPageRoleRel() { RoleID = role.ID == new Guid() ? (Guid?)null : role.ID, PageID = page.ID }); } if (!role.Value && rel != null) { db.CMSPageRoleRels.DeleteOnSubmit(rel); } } try { db.SubmitChanges(); } catch (Exception e) { } CMSPage.FullPageTable = null; if (isNew) { var another = db.CMSPages.FirstOrDefault(x => x.ID != page.ID && x.Type == page.Type && !x.Deleted); if (another != null) { var views = db.CMSPageCellViews.Where(x => x.PageID == another.ID).ToList(); foreach (var view in views) { var v = new CMSPageCellView() { Action = view.Action, Controller = view.Controller, CellID = view.CellID, Path = view.Path, Description = view.Description, OrderNum = view.OrderNum, PageID = page.ID }; db.CMSPageCellViews.InsertOnSubmit(v); } db.SubmitChanges(); } return(new ContentResult() { Content = "<script type='text/javascript'>resetSelectNode('" + "#x" + page.ID + "');</script>" }); } if (exist) { return(PartialView(page)); } return(PartialView(null)); // return RedirectToAction("Index"); }