public ViewResult AddOrEditBlogPost(int? id) { var model = new AdminBlogFull(); //pass empty model to view for new post if (id == null) { model.Date = DateTime.Now.ToString("d"); return View(model); } //otherwise, pass current blog data to be edited var post = _blogRepo.GetBlogPost((int)id); model.Date = post.Date.ToString("d"); model.Id = post.Id; model.Text = post.Text; model.Title = post.Title; model.RouteName = post.RouteName; return View(model); }
public ActionResult AddOrEditBlogPost(AdminBlogFull model) { if (ModelState.IsValid) { //convert data format var post = new BlogPost { Date = DateTime.Parse(model.Date), Id = model.Id, Text = model.Text, Title = model.Title, RouteName = model.RouteName }; _blogRepo.SaveBlogPost(post); return RedirectToAction("Blog"); } //if model not valid, send user back to form with model return View(model); }
public void CannotSaveInvalidBlogChanges() { var target = GetAdminController(); target.ModelState.AddModelError("error", "error"); var post = new AdminBlogFull { Id = 3, Title = "A New Project", Date = DateTime.Now.ToString("d"), RouteName = "sample-post", Text = "this is a sample post" }; var result = target.AddOrEditBlogPost(post); blogMock.Verify(m => m.SaveBlogPost(It.IsAny<BlogPost>()), Times.Never); Assert.IsInstanceOfType(result, typeof(ViewResult)); }