public ActionResult NewPost(PostModel model) { model.Id = Guid.NewGuid(); model.Time = DateTime.Now; if (ModelState.IsValid) { if (model.Text.ToLower().IndexOf("<script>") != -1 || model.Text.ToLower().IndexOf("style") != -1) { if (Request.IsAjaxRequest()) { return(PartialView("NewPost")); } } //string Result; if (model.AddPost(User.Identity.Name.ToString())) { return(RedirectToAction("MyPosts", "Post")); } else { if (Request.IsAjaxRequest()) { return(PartialView("NewPost")); } return(View()); } } if (Request.IsAjaxRequest()) { return(PartialView("NewPost")); } return(View()); }
public IActionResult Create(Post NewPost) { PostModel Post = new PostModel(); if (ModelState.IsValid) { if (String.IsNullOrWhiteSpace(NewPost.Author)) { NewPost.Author = "Anonym"; } Post.AddPost(NewPost); ModelState.Clear(); ViewData["Message"] = "Inlägg skapat!"; if (NewPost.RememberMe) { if (HttpContext.Request.Cookies.ContainsKey("name")) { HttpContext.Response.Cookies.Delete("name"); } HttpContext.Response.Cookies.Append("name", NewPost.Author); } } return(View()); }
public ActionResult Index(PostModel model, FormCollection frm) { if (ModelState.IsValid) { try { ViewBag.Error = ""; if (string.IsNullOrEmpty(model.PostText)) { ViewBag.Error = "ERROR: Post data can not be empty."; return(View()); } else if (!string.IsNullOrEmpty(model.PostText) && model.PostText.Length > 250) { ViewBag.Error = "ERROR: Post data can not be grater that 250."; return(View()); } int userId = Convert.ToInt32(User.Identity.Name); model.AddPost(model, userId); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } catch (Exception ex) { } } else { ModelState.AddModelError("", "Post data is not correct"); } return(RedirectToAction("Index")); }