public ActionResult EditInformation(ArticleViewModel model) { if (ModelState.IsValid) { if (model.Id == 0) { if (string.IsNullOrEmpty(model.Image.FileName)) { ModelState.AddModelError("", "Chưa add hình ảnh"); return View(model); } model.Image.SaveAs(HttpContext.Server.MapPath("~/images/upload/" + model.Image.FileName)); var id = _dtx.ContentImages.Count(); var page = new Article() { CategoryId = model.CategoryId, BodyContent = model.BodyContent, CreateDate = DateTime.UtcNow.AddHours(7), MetaDescription = model.MetaDescription, MetaKeyword = model.MetaKeyword, MetaTitle = model.MetaTitle, Permalink = "information", ShortDescription = model.ShortDescription, Title = model.Title, UserCreate = SessionManager.Admin.UserName }; _dtx.Articles.Add(page); _dtx.ContentImages.Add(new ContentImage { AltText = model.ImageAltText, Description = model.ImageDescription, MetaTitle = model.ImageMetaTitle, OriginalName = model.Image.FileName, ResizeName = model.Image.FileName, ArticleId = page.Id, Id = id }); var result = _dtx.SaveChanges(); if (result > 0) return RedirectToAction("listarticle", "admin"); ModelState.AddModelError("", "Thêm bài bị lỗi! Vui lòng kiểm tra lại"); } else { var cat = _dtx.Categories.SingleOrDefault(m => m.Permalink.ToLower() == "information"); if (cat == null) return RedirectToAction("Dashboard"); var article = _dtx.Articles.SingleOrDefault(m => m.CategoryId == cat.Id); if (article != null) { article.BodyContent = model.BodyContent; article.MetaDescription = model.MetaDescription; article.MetaKeyword = model.MetaKeyword; article.MetaTitle = model.MetaTitle; article.ShortDescription = model.ShortDescription; article.Title = model.Title; article.UserUpdate = SessionManager.Admin.UserName; article.UpdateDate = DateTime.UtcNow.AddHours(7); if (model.Image != null && !string.IsNullOrEmpty(model.Image.FileName)) { model.Image.SaveAs(HttpContext.Server.MapPath("~/images/upload/" + model.Image.FileName)); _dtx.ContentImages.Add(new ContentImage { AltText = model.ImageAltText, Description = model.ImageDescription, MetaTitle = model.ImageMetaTitle, OriginalName = model.Image.FileName, ResizeName = model.Image.FileName, ArticleId = article.Id }); } } var result = _dtx.SaveChanges(); if (result > 0) return RedirectToAction("listarticle", "admin"); ModelState.AddModelError("", "Chỉnh sửa bài bị lỗi! Vui lòng kiểm tra lại"); } } return View(model); }
public ActionResult EditArticles(int? id) { if (id != null) { var page = _dtx.Articles.SingleOrDefault(m => m.Id == id); if (page != null) { var category = _dtx.Categories.Where(m => m.Permalink.ToLower() != "home-page" && m.Permalink.ToLower() != "information").ToList().Select(source => new SelectListItem() { Selected = source.Id == page.CategoryId, Text = source.Name, Value = source.Id.ToString() }).ToList(); var model = new ArticleViewModel() { BodyContent = page.BodyContent, CategoryId = page.CategoryId, Id = page.Id, MetaDescription = page.MetaDescription, MetaKeyword = page.MetaKeyword, MetaTitle = page.MetaTitle, ShortDescription = page.ShortDescription, Title = page.Title, Category = category }; return View(model); } } var returnModel = new ArticleViewModel() { Category = _dtx.Categories.Where(m => m.Permalink.ToLower() != "home-page" && m.Permalink.ToLower() != "information").ToList().Select(source => new SelectListItem() { Text = source.Name, Value = source.Id.ToString(CultureInfo.InvariantCulture) }).ToList() }; return View(returnModel); }
public ActionResult EditInformation() { ArticleViewModel model; var categoryInfo = _dtx.Categories.SingleOrDefault(m => m.Permalink == "information"); if (categoryInfo == null) return RedirectToAction("Dashboard"); var page = _dtx.Articles.SingleOrDefault(m => m.CategoryId == categoryInfo.Id); if (page != null) { model = new ArticleViewModel() { Id = page.Id, BodyContent = page.BodyContent, CategoryId = page.CategoryId, MetaDescription = page.MetaDescription, MetaKeyword = page.MetaKeyword, MetaTitle = page.MetaTitle, Permalink = page.Permalink, ShortDescription = page.ShortDescription, Title = page.Title }; } else { var category = _dtx.Categories.SingleOrDefault(m => m.Permalink == "information"); if (category != null) model = new ArticleViewModel { CategoryId = category.Id, Category = new List<SelectListItem>() { new SelectListItem() { Selected = true, Text = category.Name, Value = category.Id.ToString(CultureInfo.InvariantCulture) } } }; else return RedirectToAction("listarticle", "admin"); } return View(model); }