In a book by Scott Hanselman (chapter 1), he offers us two implementation options [HttpPost] for creating an action method.
The first of these relies on TryUpdateModel to update the model object based on the input form fields. When the fields in the input form contain invalid input, ModelState.IsValid will be set to false.
[HttpPost] public ActionResult Create(FormCollection collection) { Dinner dinner = new Dinner(); if (TryUpdateModel(dinner)) { dinnerRepository.Add(dinner); dinnerRepository.Save(); return RedirectToAction("Details", new { id = dinner.DinnerId }); } else return View(dinner); }
The second method is to use the model passed as the arg creation method, as follows:
[HttpPost] public ActionResult Create(Dinner dinner) { if (ModelState.IsValid) { dinnerRepository.Add(dinner); dinnerRepository.Save(); return RedirectToAction("Details", new { id = dinner.DinnerId }); } else return View(dinner); }
Which one is recommended for use in production?
asp.net-mvc asp.net-mvc-2
xport
source share