[ACCEPTED]-When and why do you use TryUpdateModel in asp.net mvc 2?-asp.net-mvc

Accepted answer
Score: 50

You can use this method to update the model 28 that backs a particular view via the given 27 controller. For example, if I have a view 26 displaying a Foo object with property Bar 25 populated by a textbox, I can call a method 24 Save() on the controller and call TryUpdateModel 23 to attempt to update the Foo.

public class Foo {
  public string Bar { get; set; }
}

// ... in the controller
public ActionResult Save() {
  var myFoo = new Foo();
  TryUpdateModel(myFoo);
}

This will try 22 to update the model with the given value 21 for Bar. If the update fails validation 20 (say, for example, that Bar was an integer 19 and the textbox had the text "hello" in 18 it) then TryUpdateModel will pass update 17 the ViewData ModelState with validation 16 errors and your view will display the validation 15 errors.

Make sure you pay close attention 14 to the security warning for .NET Framework 13 4 in the MSDN documentation:

Security Note Use one of 12 the [Overload:System.Web.Mvc.Controller.TryUpdateModel``1] methods 11 that takes either a list of properties 10 to include (a whitelist) or a list of 9 properties to exclude (a blacklist). If 8 no explicit whitelist or blacklist is 7 passed, the [Overload:System.Web.Mvc.Controller.TryUpdateModel`1] method 6 tries to update every public property 5 in the model for which there is a corresponding 4 value in the request. A malicious user 3 could exploit this in order to update properties 2 that you do not intend to provide access 1 to.

https://msdn.microsoft.com/en-us/library/system.web.mvc.controller.tryupdatemodel(v=vs.100).aspx

Score: 17

TryUpdateModel() allows you to bind parameters to your model 11 inside your action. This is useful if you 10 want to load your model from a database 9 then update it based on user input rather 8 than taking the entire model from user input.

public ActionResult Update(int id) {
    var service = new ServiceClass();
    var record = service.LoadModel(id);
    if (!TryUpdateModel(record)) {
        // There was an error binding data
        return View();
    }
    // Everything was ok, now save the record back to the database
    service.SaveModel(record);
    return View("Success");
}

It 7 acts similar to UpdateModel()in this respect but returns 6 true on success and false if there is an 5 error. UpdateModel() throws an exception if there is 4 an error which requires a bit more code.

Note: You 3 might want to use one of the overloads that 2 allows you to limit which properties can 1 be updated.

Score: 13

We also used TryUpdateModel to avoid Model Binding magic 8 before the Action was called; instead we 7 took a HttpFormCollection as our parameter and called TryUpdateModel within 6 the method. The clean boolean value returned 5 from this allowed control flow to be passed 4 to a Success or Failure method for the Action. e.g.

public ActionResult Save(HttpFormCollection formCollection)
{
  var saveModel = new SaveModel(); // or from a Factory etc
  var validModel = TryUpdateModel(_saveModel, formCollection); // order may be incorrect
  return validModel ? Save(saveModel) : InvalidSaveModel(saveModel);
}

We 3 found it quite easy to build a HttpFormCollection for all 2 our validation cases and therefore test 1 the action.

More Related questions