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?
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.
You can check the Request. HttpMethod property.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With