public ActionResult AddArticle() { var model = new HelpAndAdviceDetailModel(); using (var context = new ApplicationDbContext()) { var categories = context.HelpAndAdviceCategories.Select(x => new CategoryDropDown { Key = x.Id.ToString(), Value = x.Category }).ToList(); ViewBag.Categories = categories; } return(View(model)); }
public ActionResult EditArticle(int Id) { using (var context = new ApplicationDbContext()) { var articleInfo = context.HelpAndAdviceDetails.Where(y => y.Id == Id).FirstOrDefault(); var articleModel = new HelpAndAdviceDetailModel { CategoryId = articleInfo.CategoryId, Content = articleInfo.Content, Heading = articleInfo.Heading, Id = articleInfo.Id, SubHeading = articleInfo.SubHeading }; var categories = context.HelpAndAdviceCategories.Select(x => new CategoryDropDown { Key = x.Id.ToString(), Value = x.Category }).ToList(); ViewBag.Categories = categories; return(View(articleModel)); } }
public ActionResult AddArticle(HelpAndAdviceDetailModel model) { if (ModelState.IsValid) { using (var context = new ApplicationDbContext()) { if ((model.Id == 0) || (model.Id == null)) { var detailContext = new HelpAndAdviceDetail { Content = model.Content, CategoryId = model.CategoryId, Heading = model.Heading, SubHeading = model.SubHeading, LastUpdated = DateTime.Now }; context.HelpAndAdviceDetails.Add(detailContext); } else { var detailContext = context.HelpAndAdviceDetails.Where(y => y.Id == model.Id).FirstOrDefault(); detailContext.Content = model.Content; detailContext.CategoryId = model.CategoryId; detailContext.Heading = model.Heading; detailContext.SubHeading = model.SubHeading; detailContext.LastUpdated = DateTime.Now; } context.SaveChanges(); } return(RedirectToAction("ViewArticle")); } else { return(View()); } }
public ActionResult ViewArticle() { using (var context = new ApplicationDbContext()) { var articles = new List <HelpAndAdviceDetailModel>(); var articlesContext = context.HelpAndAdviceDetails.ToList(); foreach (var item in articlesContext) { var article = new HelpAndAdviceDetailModel { Id = item.Id, Heading = item.Heading, SubHeading = item.SubHeading, Content = item.Content, CategoryId = item.CategoryId }; articles.Add(article); } return(View(articles)); } }