tags:

views:

1385

answers:

4

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.UpdateModelTModel at System.Web.Mvc.Controller.UpdateModelTModel 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?

+9  A: 

Examine the controller.ModelState and look for entries with Errors > 0.

Craig Stuntz
+12  A: 

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.

Ariel Popovsky
+1  A: 

It's hard to say for sure without seeing any code, but every time I've seen an exception of that type, 99% of the time it's been database related. Not to say the root cause isn't somewhere in the code, but it's quite possible you're missing something and trying to pass invalid data to the database. Other issues to look for would be any relationship handling that needs to take place.

dhulk
A: 

I generally check 3 things.

  1. Do each of the models members have getters and setters
  2. Am I fulfilling the requirements of the model. ie: Is all required data present and correct?
  3. Am I violating a relationship somewhere? Do I need to have default values on nested objects?

If that still fails then i revert to;

class MyClass(FormCollection collection)
{
  string a = collection["MyField"];

which usually works.

griegs