// GET: Articles/Details/5 public ActionResult Details(int?id, string user, string deleteMessage, string editMessage) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Article article = db.Articles.Find(id); if (article == null) { return(HttpNotFound()); } if (deleteMessage == "failed") { ViewBag.DeleteMessage = "This isn't yours to delete!"; } if (editMessage == "failed") { ViewBag.DeleteMessage = "This isn't yours to edit!"; } var commentsList = (from t in db.Comments where t.ArticleID == article.ID orderby t.ArticleID select t).ToList().OrderBy(o => o.ID).ToList(); var articleAndComments = new ArticleAndComments { CommentsList = commentsList, Article = article, Comment = new Comment() }; return(View(articleAndComments)); }
public ActionResult Details(ArticleAndComments articleAndComments, string user) { //var articleID = articleAndComments.Comment.ArticleID; //articleAndComments.Article = db.Articles.Find(articleID); //articleAndComments.Comment.Article = articleAndComments.Article; //var commentsList = (from t in db.Comments // where t.ArticleID == articleID // orderby t.ArticleID // select t).ToList().OrderBy(o => o.ID).ToList(); //articleAndComments.CommentsList = commentsList; if (ModelState.IsValid) { if (string.IsNullOrWhiteSpace(articleAndComments.Comment.Author)) { articleAndComments.Comment.Author = "Anonymous"; } Regex rgx = new Regex("[^a-zA-Z0-9!£$€%^&*()-_+={[}]:;@'~#,|\\.?/ ¬`¦\"]"); articleAndComments.Comment.Text = rgx.Replace(articleAndComments.Comment.Text, ""); db.Comments.Add(articleAndComments.Comment); db.SaveChanges(); return(RedirectToAction("Details", new { id = articleAndComments.Comment.ArticleID, user = user, deleteMessage = "" })); } else { var errors = ModelState.Select(x => x.Value.Errors) .Where(y => y.Count > 0) .ToList(); var tesat = 1; } //return RedirectToAction("Details", new { id = articleAndComments.Comment.ArticleID, user = user, deleteMessage = "" }); articleAndComments.CommentsList = (from t in db.Comments where t.ArticleID == articleAndComments.Comment.ArticleID orderby t.ArticleID select t).ToList().OrderBy(o => o.ID).ToList(); articleAndComments.Article = db.Articles.Find(articleAndComments.Comment.ArticleID); return(View(articleAndComments)); }
// GET: Articles/Delete/5 public ActionResult Delete(int?id, string user) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Article article = db.Articles.Find(id); if (article == null) { return(HttpNotFound()); } //if allowed to delete if ((user == article.Author) || string.IsNullOrWhiteSpace(article.Author) || article.Author == "Anonymous") { ArticleAndComments articleAndComments = new ArticleAndComments(); articleAndComments.Article = article; return(View(articleAndComments)); } //if not allowed to delete return(RedirectToAction("Details", new { id = article.ID, user = user, deleteMessage = "failed" })); }