This method can be used to update a model that supports a specific view through this controller. For example, if I have a view displaying a Foo object with a property bar filled with a text box, I can call the Save () method on the controller and call TryUpdateModel to try to update Foo.
public class Foo { public string Bar { get; set; } }
This will try to update the model with the given value for Bar. If the update does not pass the check (say, for example, that the Bar was an integer, and the text field had the text βhelloβ in it), then TryUpdateModel will transmit the ViewData ModelState update with verification errors, and verification errors will be displayed on your view.
Be sure to pay attention to the security warning for the .NET Framework 4 in the MSDN documentation:
Safety note . Use one of the [Overload: System.Web.Mvc.Controller.TryUpdateModel``1] methods that take either a property list to include (whitelist) or a property list to exclude (a blacklist). If there is an explicit whitelist or blacklist, the [Overload: System.Web.Mvc.Controller.TryUpdateModel`1] method attempts to update each publication property in the model for which the corresponding value in the request is. An attacker could use this to update properties that you are not going to grant access to.
https://msdn.microsoft.com/en-us/library/system.web.mvc.controller.tryupdatemodel(v=vs.100).aspx
Martin Doms Mar 11 2018-11-11T00: 00Z
source share