I have seen numerous examples of creating actions in articles, books, and examples. There seem to be two common styles.
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(FormCollection collection) { try { var contact = Contact.Create(); UpdateModel<Contact>(contact); contact.Save(); return RedirectToAction("Index"); } catch (InvalidOperationException ex) { return View(); } }
BUT...
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Create([Bind(Exclude="Id")]Contact contact) { try { contact.Save(); // ... assumes model does validation return RedirectToAction("Index"); } catch (Exception ex) { // ... have to handle model exceptions and populate ModelState errors // ... either here or in the model validation return View(); } }
I tried both methods and both have pros and cons, IMO.
For example, when using the FormCollection version, I have to deal with "Id" manually in my connecting device, since Bind / Exclude does not work here. With a typed version of a method, I donβt use model binding at all. I like to use the model binder because it allows me to fill in ModelState errors without having any knowledge of ModelState in my model validation module.
Any ideas?
Update: I answered my question, but I will not mark it as an answer for a few days if someone has a better answer.
validation asp.net-mvc
Sailing judo
source share