2010-03-29 88 views
0

我们正在将我们的ASP.NET MVC 1.0 web应用程序迁移到MVC 2.0,但我们遇到了一个小问题。从MVC 1.0迁移到MVC 2.0后,UpdateModel()失败

在我们的报告创建向导中,可能会留下标题文本框为空,并将其填入通用标题(在发布操作中)。

,做了标题的模型更新的代码是:

  if (TryUpdateModel(reportToEdit, new[] { "Title" })) 
      { 
       //all ok here try to create (custom validation and attach to graph to follow) 

       //if title is empty get config subject 
       if (reportToEdit.Title.Trim().Length <= 0) 
        reportToEdit.Title = reportConfiguration.Subject; 

       if (!_service.CreateReport(1, reportToEdit, SelectedUser.ID, reportConfigID, reportCategoryID, reportTypeID, deviceUnitID)) 
        return RedirectToAction("Index"); 
      } 

在MVC 1.0,这正常工作,该reportToEdit有一个空头衔,如果文本框为空,然后将其与填充Subject属性。

在MVC 2.0中失败/返回false。

如果我添加了线以上:

UpdateModel(reportToEdit, new[] { "Title" }); 

它抛出

System.InvalidOperationException was unhandled by user code 
    Message="The model of type 'Footprint.Web.Models.Reports' could not be updated." 
    Source="System.Web.Mvc" 
    StackTrace: 
     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, String[] includeProperties) 
     at Footprint.Web.Controllers.ReportsController.Step1(FormCollection form) in C:\TFS Workspace\ExtBusiness_Footprint\Branches\apitts_uioverhaul\Footprint\Footprint.Web\Controllers\ReportsController.cs:line 398 
     at lambda_method(ExecutionScope , ControllerBase , Object[]) 
     at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) 
     at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) 
     at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) 
     at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a() 
     at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) 
    InnerException: 

读MVC2发行说明我看到这个重大更改:

每个属性的模型对象不管是否设置了新值,都使用IDataErrorInfo来执行验证。在ASP.NET MVC 1.0中,只有具有新值的属性才会被验证。在ASP.NET MVC 2中,仅当所有属性验证器成功时才调用IDataErrorInfo的Error属性。

但我很困惑这是如何影响我。我正在使用实体框架生成的类。

任何人都可以指出为什么这是失败?

回答

0

好的,我已经使用了一点工作来完成这项工作。

而不是使用FormCollectionTryUpdateModel()的隐式绑定,我现在修改表单,然后将其传递到TryUpdateModel()

这是现在的代码:

  if (string.IsNullOrEmpty(form["Title"])) 
       form["Title"] = reportConfiguration.Subject; 

      //update model with remaining forms collection: serialnumber, description, gasfactor, samplerate, checkforpolling 
      if (TryUpdateModel(reportToEdit, new[] { "Title" }, form)) 
      { 
       ...etc 

希望这是有用的人:)

+0

这是使用属性白名单中TryUpdateModel方法,是不是?我发现的问题(在MVC 1中,至少可能已经在MVC 2+中修复了)是,如果你有一个更复杂的对象结构并且链接了EntityRef 对象,那么TryUpdateModel会更新它们,除非包含白名单条目他们的属性),在这种情况下整个“子”对象的更新被忽略。显然,这是“通过设计” - 即我们CBA得到这个工作。对我来说,最烦人的是它会非常有用 – 2012-03-02 10:09:08