views:

669

answers:

2

I'm trying to implement MVC 2 RC version, the latest release of ASP.Net MVC and it can't do a simple Controller.UpdateModel(object) without throwing this exception:

The model of type '[Insert namespace of object being updated here]' could not be updated.

InvalidOperationException

Here's the stack trace:

at System.Web.Mvc.Controller.UpdateModel[TModel](TModel model, String prefix, String[] includeProperties, String[] excludeProperties, IValueProvider valueProvider) at System.Web.Mvc.Controller.UpdateModel[TModel](TModel model) at Ccis.Cgov360.Web.InternalApp.Controllers.AdminController.MailingLabelTypeSelected() in C:\Projects\Meadowlark\Development\Meadowlark\Applications\InternalApp\Controllers\AdminController.cs:line 1528 at lambda_method(ExecutionScope , ControllerBase , Object[] ) at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) at System.Web.Mvc.ControllerActionInvoker.<>c_DisplayClassd.b_a() at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)

When I use MVC Preview 2 it functions and updates the model just fine with no exceptions thrown. I saw elsewhere that there is a bug in RC version, is this the same thing?

I've spent way too much time trying to fix this issue. I was hoping to get the RC release so that we can start using the Html helpers such as TextBoxFor<>, CheckBoxFor<>, etc. and the client-side validation.

+2  A: 

To add to Levi's comment, if you catch the exception and return the Edit view, you should see the validation message for the field(s) failing validation, assuming your view contains:

<%= Html.ValidationMessageFor(model => model.name) %>

And your Controller Edit action would contain...

     try {
         UpdateModel(entity, new [] {  "name", "address1", "address2", "city", "state", "zip" } );
         TempData["Message"] = "Success";
         return RedirectToAction("List");
     }
     catch {
         TempData["Message"] = "Error saving form";
         return View(entity);
     }
mrjoltcola
+2  A: 

Hi.

I also ran into this issue.

As a work-around, I am just calling TryUpdateModel() instead of UpdateModel().

Rami A.