public ActionResult CreateEdit(NccPage model, string PageContent, long ParentId, string SubmitType) { ViewBag.MessageType = "ErrorMessage"; ViewBag.Message = "Error occoured. Please fill up all field correctly."; model.Content = PageContent; if (ModelState.IsValid) { if (model.Title.Trim() == "") { ViewBag.Message = "Please enter page title."; } else if (model.Slug.Trim() == "") { ViewBag.Message = "Please do not delete slug. Slug is required."; } else { var slugPage = _pageService.GetBySlugs(model.Slug); if (model.Id > 0) { try { var parrent = _pageService.Get(ParentId); model.Parent = parrent; } catch (Exception) { } if (slugPage != null && slugPage.Id != model.Id) { ViewBag.Message = "This slug is already used in another page."; } else { try { _pageService.Update(model); ViewBag.MessageType = "SuccessMessage"; ViewBag.Message = "Page updated successful"; } catch (Exception ex) { _logger.LogError("Page create error.", ex.ToString()); } } } else { if (slugPage != null) { ViewBag.Message = "Duplicate slug is found."; } else { try { _pageService.Save(model); ViewBag.MessageType = "SuccessMessage"; ViewBag.Message = "Page save successful"; } catch (Exception ex) { _logger.LogError("Page create error.", ex.ToString()); } } } } } if (SubmitType.ToLower() == "publish") { return(RedirectToAction("Manage")); } ViewBag.Layouts = GlobalConfig.ActiveTheme.Layouts; ViewBag.AllPages = _pageService.LoadAll().Where(p => p.Status == (int)NccPage.NccPageStatus.Published && p.Id != model.Id); return(View(model)); }
public ActionResult CreateEdit(NccPage model, long ParentId, string SubmitType) { ViewBag.MessageType = "ErrorMessage"; ViewBag.Message = "Error occoured. Please fill up all field correctly."; if (ModelState.IsValid) { bool isSuccess = true; #region For default language var defaultPageDetails = model.PageDetails.Where(x => x.Language == GlobalContext.WebSite.Language).FirstOrDefault(); if (defaultPageDetails == null) { isSuccess = false; ViewBag.Message = "Default language Page can't be null"; } else { //title empty validation if (string.IsNullOrEmpty(defaultPageDetails.Title)) { isSuccess = false; ViewBag.Message = "Default language Title can't be null"; } //slug empty validation if (isSuccess && string.IsNullOrEmpty(defaultPageDetails.Slug)) { isSuccess = false; ViewBag.Message = "Default language Slug can't be null"; } //slug duplicate validation if (isSuccess) { defaultPageDetails.Title = defaultPageDetails.Title.Trim(); defaultPageDetails.Slug = defaultPageDetails.Slug.Trim(); if ((defaultPageDetails.MetaDescription == null || defaultPageDetails.MetaDescription.Trim() == "") && defaultPageDetails.Content != null && defaultPageDetails.Content.Length > 0) { defaultPageDetails.MetaDescription = Regex.Replace(defaultPageDetails.Content, "<.*?>", String.Empty); if (defaultPageDetails.MetaDescription.Length > 161) { defaultPageDetails.MetaDescription = defaultPageDetails.MetaDescription.Substring(0, 160); } } model.Name = defaultPageDetails.Slug; var temp = _pageDetailsService.Get(defaultPageDetails.Slug, defaultPageDetails.Language); if (temp != null && temp.Id != defaultPageDetails.Id) { isSuccess = false; ViewBag.Message = "Duplicate Slug found for language " + defaultPageDetails.Language; } } } #endregion #region Check validation for other languages List <NccPageDetails> deletedList = new List <NccPageDetails>(); foreach (var item in model.PageDetails.Where(x => x.Language != GlobalContext.WebSite.Language).ToList()) { if (item.Id == 0 && string.IsNullOrEmpty(item.Title) && string.IsNullOrEmpty(item.Slug) && string.IsNullOrEmpty(item.Content) && string.IsNullOrEmpty(item.MetaKeyword) && string.IsNullOrEmpty(item.MetaDescription)) { deletedList.Add(item); } else { if (string.IsNullOrEmpty(item.Title)) { isSuccess = false; ViewBag.Message = "Title can't be null for language " + item.Language; } //slug empty validation if (isSuccess && string.IsNullOrEmpty(item.Slug)) { isSuccess = false; ViewBag.Message = "Slug can't be null for language " + item.Language; } //slug duplicate validation if (isSuccess) { item.Title = item.Title.Trim(); item.Slug = item.Slug.Trim(); if ((item.MetaDescription == null || item.MetaDescription.Trim() == "") && item.Content != null && item.Content.Length > 0) { item.MetaDescription = Regex.Replace(item.Content, "<.*?>", String.Empty); if (item.MetaDescription.Length > 161) { item.MetaDescription = item.MetaDescription.Substring(0, 160); } } var temp = _pageDetailsService.Get(item.Slug, item.Language); if (temp != null && temp.Id != item.Id) { isSuccess = false; ViewBag.Message = "Duplicate Slug found for language " + item.Language; } } } } //Remove empty if (isSuccess) { foreach (var item in deletedList) { model.PageDetails.Remove(item); } } #endregion #region Operation if (isSuccess) { try { var parrent = _pageService.Get(ParentId); model.Parent = parrent; } catch (Exception) { } if (model.Id > 0) { try { _pageService.Update(model); ViewBag.MessageType = "SuccessMessage"; ViewBag.Message = "Page updated successful"; } catch (Exception ex) { _logger.LogError("Page create error.", ex.ToString()); } } else { try { _pageService.Save(model); ViewBag.MessageType = "SuccessMessage"; ViewBag.Message = "Page save successful"; } catch (Exception ex) { _logger.LogError("Page create error.", ex.ToString()); } } } #endregion } else { ViewBag.Message = string.Join("; ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)); } if (ViewBag.MessageType == "SuccessMessage" && SubmitType.ToLower() == "publish") { return(RedirectToAction("Manage")); } SetPageViewData(model); return(View(model)); }