Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is more correct: using UpdateModel() or receiving a model as a parameter?

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.

like image 853
Sailing Judo Avatar asked Jul 17 '09 13:07

Sailing Judo


People also ask

What is the difference between UpdateModel () and TryUpdateModel ()?

UpdateModel() throws an exception, if validation fails, whereas TryUpdateModel() will never throw an exception. The similarity between both is that the functions are used to update the model with the form values and perform the validations.

What is use of UpdateModel in MVC?

UpdateModel() is a Controller helper method that attempts to bind a bunch of different input data sources (HTTP POST data coming from a View, QueryString values, Session variables/Cookies, etc.) to the explicit model object you indicate as a parameter. Essentially, it is only for model binding.

What is a ViewModel in MVC?

In ASP.NET MVC, ViewModel is a class that contains the fields which are represented in the strongly-typed view. It is used to pass data from controller to strongly-typed view.


1 Answers

Use UpdateModel when you want to update an already present model object, which you may get from database or you want to instantiate the model object in some specific way

eg:

 [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult EditEmployee(int id, FormCollection collection)
    {

try
     {

    Contact contact = repository.getContact(id);
    UpdateModel(contact, collection.ToValueProvider());
    repository.save();
    return RedirectToAction("Index");

}

    catch
    {
    //Handle 
    return View();
    }

}

If you dont have the above requirements then put it as action parameter.

like image 79
San Avatar answered Oct 05 '22 03:10

San