Asp.net-mvc – MVC Models not successfully updated but can’t find reason

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?

Best Answer

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.