Funky81
Funky81

Reputation: 1677

UpdateModel vs TryUpdateModel

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 ?

Upvotes: 15

Views: 13348

Answers (2)

dasun
dasun

Reputation: 25

Main Difference Updatemodel() will throws an exception if validation fails But in TryUpdatemodel() will never throws an exception when validation fails

Similarity Both are used to update model with form values and perform validations

Upvotes: 1

antonioh
antonioh

Reputation: 2944

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.

Upvotes: 20

Related Questions