Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Models not successfully updated but can't find reason

Tags:

asp.net-mvc

This has been driving me nuts.

I keep getting the following exception

System.InvalidOperationException: The model of type 'Models.Expense' was not successfully updated. at System.Web.Mvc.Controller.UpdateModel[TModel](TModel model, String prefix, String[] includeProperties, String[] excludeProperties, IDictionary`2 valueProvider) at System.Web.Mvc.Controller.UpdateModel[TModel](TModel model) atMVC.Controllers.BaseExpenseController.Edit(String id, FormCollection collection) in C:\Projects\Expenses.MVC\Controllers\BaseExpenseController.cs:line 109

But I can't track down why it is not updating, nothing in the exception suggests why it has not updated.

Any pointers?

like image 462
Coppermill Avatar asked Jul 07 '09 14:07

Coppermill


People also ask

How does model update view in MVC?

In a web MVC the model does not update any view! The model should know nothing about the external world. Instead: (a) Controller updates the model and then reads the data from it, in order to pass it further to the view for displaying it; or (b) Controller updates the model.

How can we detect that an MVC controller is called by post or get?

You can check the Request. HttpMethod property.


1 Answers

Catch the exception or call TryUpdateModel instead. TryUpdateModel won't throw an exception if it can't update your model, it will just return false. You'll find the error details in the ModelState as suggested by Craig. In fact UpdateModel just calls TryUpdateModel and throws if it returns false.

like image 170
Ariel Popovsky Avatar answered Oct 07 '22 01:10

Ariel Popovsky