Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 275
Pranu Avatar asked Nov 03 '11 14:11

Pranu


2 Answers

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.

like image 55
itsmatt Avatar answered Sep 27 '22 16:09

itsmatt


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)

like image 22
Adam Tuliper Avatar answered Sep 27 '22 18:09

Adam Tuliper