Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UpdateModel vs TryUpdateModel

Tags:

asp.net-mvc

I've some simple questions regarding asp.net mvc development.

What the use of UpdateModel and TryUpdateModel ? and which condition does apply to use either UpdateModel or TryUpdateModel. From my experience TryUpdateModel beside binding FormCollection into Model also validate the data. Is it correct ?

like image 827
Funky81 Avatar asked Mar 11 '09 22:03

Funky81


People also ask

What is the difference between UpdateModel and TryUpdateModel in MVC?

The difference between the two methods is that UpdateModel will throw an exception if validation fails and TryUpdateModel will inform about the validation result in a boolean.

What is TryUpdateModel?

TryUpdateModel<TModel>(TModel, String, String[], IValueProvider) Updates the specified model instance using values from the value provider, a prefix, and included properties.

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 MVC life cycle in C#?

The life cycle is basically is set of certain stages which occur at a certain time. Application Life Cycle. MVC actually defined in two life cycles, the application life cycle, and the request life cycle. The application life cycle, in which the application process starts the running server until the time it stops.


1 Answers

You're right, both methods are used to update the Model with the Form values and perform the validations. There's a default binder but you can build custom ones if needed.

The difference between the two methods is that UpdateModel will throw an exception if validation fails and TryUpdateModel will inform about the validation result in a boolean.

like image 104
antonioh Avatar answered Oct 19 '22 13:10

antonioh