I've seen numerous examples of create actions in articles, books, and examples. It seem there are two prevalent 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();
}
}
And...
[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's validation
return View();
}
}
I've tried both methods and both have pluses and minuses, IMO.
For example, when using the FormCollection version I have to deal with the "Id" manually in my model binder as the Bind/Exclude doesn't work here. With the typed version of the method I don't get to use the model binder at all. I like using the model binder as it lets me populate the ModelState errors without having any knowledge of the ModelState in my model's validation code.
Any insights?
Update: I answered my own question, but I wont mark it as answered for a few days in case somebody has a better answer.