Pranu
Pranu

Reputation: 121

the model of type could not be updated mvc

i am trying to update model with some changes like below

inventory.SiteId=Siteitem.Siteid;
inventory.CreatedBy=currentuser;
inventory.CreatedDate=DateTime.Today;

TryUpdateModel(inventory);

or

UpdateModel(inventory);

both are failed to update the model

Upvotes: 12

Views: 7874

Answers (2)

Adam Tuliper
Adam Tuliper

Reputation: 30152

Remember here that TryUpdateModel updates the model with values from your form. Are you trying to update the model in your database? If so you need to specify this. If you set inventory.Whatever above, your model is already set with those values.

The only time to call TryUpdateModel is when you want to take those form values and put them into the model and set ModelState (which will then contain any errors as part of this process as well)

Upvotes: 1

itsmatt
itsmatt

Reputation: 31416

So the question is "Why is it failing?" One thing you could look at is the ModelState dictionary and see if the model is is valid and if there are errors. There's a reason it's failing, of course. I think that would be one of my first checks. Maybe you've got a mismatch of some sort - integer on one side and string on the other, for instance.

Upvotes: 22

Related Questions