我正在尝试实现MVC 2 RC版本,ASP.Net MVC的最新版本,它不能做一个简单的Controller.UpdateModel(对象)而不会抛出此异常:
无法更新“[在此处更新对象的插入名称空间]”类型的模型。
出现InvalidOperationException
这是堆栈跟踪:
at System.Web.Mvc.Controller.UpdateModel [TModel](TModel model,String prefix,String [] includeProperties,String [] excludeProperties,IValueProvider valueProvider)
在System.Web.Mvc.Controller.UpdateModel [TModel](TModel模型)
在C:\ Projects \ Meadowlark \ Development \ Meadowlark \ Applications \ InternalApp \ Controllers \ AdminController.cs中的Ccis.Cgov360.Web.InternalApp.Controllers.AdminController.MailingLabelTypeSelected():第1528行
在lambda_method(ExecutionScope,ControllerBase,Object [])
在System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase控制器,Object []参数)
在System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary 2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2个参数)
在System.Web.Mvc.ControllerActionInvoker。<> c__DisplayClassd.b__a()
在System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter过滤器,ActionExecutingContext preContext,Func`1 continuation)
当我使用MVC Preview 2时,它可以正常运行并更新模型,不会抛出任何异常。我在其他地方看到RC版本中有一个错误,这是一回事吗?
我花了太多时间试图解决这个问题。我希望获得RC版本,以便我们可以开始使用Html帮助程序,如TextBoxFor<&gt ;, CheckBoxFor<>等等,以及客户端验证。
答案 0 :(得分:3)
要添加到Levi的注释,如果您捕获异常并返回编辑视图,您应该看到字段的验证消息未通过验证,假设您的视图包含:
<%= Html.ValidationMessageFor(model => model.name) %>
您的Controller Edit操作将包含...
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);
}
答案 1 :(得分:2)
我也遇到了这个问题。
作为解决方法,我只是调用TryUpdateModel()而不是UpdateModel()。