public JsonResult Add(int parentId = 0) { string parentName = default(string); if (parentId != 0) { var getParentPage = _repoWebPage.DbContext.WebPages.FirstOrDefault(pg => pg.Id == parentId); if (getParentPage != null) { parentName = getParentPage.Title; } } var viewModel = new AddPageViewModel { ParentId = parentId, ParentName = parentName }; var viewEdit = RenderPartialViewToString("Add", viewModel); return Json(new JsonResponse((object)viewEdit), JsonRequestBehavior.AllowGet); }
public JsonResult Add(AddPageViewModel viewModel) { if (!ModelState.IsValid) return Json(new JsonResponse("Invalid Data", "Please provide correct details for page")); if (_repoWebPage.DbContext.WebPages.FirstOrDefault(pg => pg.Title == viewModel.Title) != null) return Json(new JsonResponse("Page exists Already!", "A page with same title/name already exists")); WebPage getPage = null; if (viewModel.ParentId != 0) { getPage = _repoWebPage.DbContext.WebPages.FirstOrDefault(pg => pg.Id == viewModel.ParentId && pg.Parent == null); if (getPage == null) return Json(new JsonResponse("Invalid parent page!", "The chosen parent page doesn't exist or is not root page.")); } var newPage = new WebPage { Title = viewModel.Title, Parent = getPage, Contents = viewModel.Contents, IsPublished = false }; _repoWebPage.DbContext.WebPages.Add(newPage); _repoWebPage.DbContext.SaveChanges(); var viewEditModel = new EditPageViewModel { PageId = newPage.Id, Title = newPage.Title, Contents = newPage.Contents, ParentId = newPage.ParentId, ParentName = (newPage.Parent != null ? newPage.Parent.Title : "") }; var modelTreeView = new PagesTreeView{CurrentPages = _repoWebPage.GetAllPages()}; return Json(new JsonResponse( new { TreeView = RenderPartialViewToString("PagesTreeView", modelTreeView), Page = RenderPartialViewToString("Edit", viewEditModel) }) { Message = "Page Added Successfully!", Description = "New Page has been added succesfully." }); }