public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } using (var db = new BlogDbContext()) { var article = db.Articles .Where(a => a.Id == id) .Include(a => a.Author) .First(); if (article == null) { return(HttpNotFound()); } var model = new MergedModels(); model.Article = article; var comments = new List <Comment>(); if (article.CommentIds != null) { var commentIds = article.CommentIds .Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries) .Select(int.Parse) .ToList(); foreach (var commentId in commentIds) { var comment = db.Comments .Where(a => a.Id == commentId) .Include(a => a.Author) .First(); comments.Add(comment); } } model.Comments = comments; return(View(model)); } }
public ActionResult Delete(MergedModels models) { var id = models.Article.Id; using (var db = new BlogDbContext()) { var article = db.Articles .Where(a => a.Id == id) .Include(a => a.Author) .First(); if (article == null) { return(HttpNotFound()); } db.Articles.Remove(article); db.SaveChanges(); return(RedirectToAction("Home")); } }
public ActionResult Details(MergedModels models) { if (ModelState.IsValid) { using (var db = new BlogDbContext()) { // Adding comment to the datebase var comment = models.Comment; comment.AuthorId = db.Users .Where(u => u.UserName == this.User.Identity.Name) .First() .Id; db.Comments.Add(comment); db.SaveChanges(); // Adding the comment id to article var article = db.Articles .Where(a => a.Id == models.Article.Id) .Include(a => a.Author) .First(); article.CommentIds += string.Format("{0}, ", db.Comments .AsEnumerable() .Last() .Id); db.Entry(article).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Details", new { id = article.Id })); } } return(View(models)); }