【问题标题】:MVC3 - UpdateModel... how to change incoming data?MVC3 - UpdateModel ...如何更改传入数据?
【发布时间】:2011-12-19 04:24:45
【问题描述】:

UpdateModel 失败,因为 arcm.Notes 进入此方法时为 null,我希望它为空字符串。

也许我需要在将 Notes 设置为 "" 后刷新 ValueProvider (?),这样我才能使用 UpdateModel。

 public ActionResult Edit1(Guid id, ActivityResponseConsumerMobile arcm) {
        if (arcm.Notes == null)
            arcm.Notes = "";

        if (!ModelState.IsValid) {
            SetupDropDowns();
            return View(arcm);
        }

        ActivityResponseConsumerMobile arcmDb = uow.ActivityResponseConsumerMobiles.Single(a => a.Id == id);
        try {
            UpdateModel(arcmDb, null, null, new[] { "Id" });

【问题讨论】:

    标签: asp.net-mvc-3 lightspeed


    【解决方案1】:

    这是我相信自 MVC2 以来的默认行为。 请注意这里的一种解决方法:

    http://brianreiter.org/2010/09/16/asp-net-mvc-2-0-undocumented-model-string-property-breaking-change/

    public sealed class EmptyStringModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { bindingContext.ModelMetadata.ConvertEmptyStringToNull = false; return base.BindModel(controllerContext, bindingContext); } }

    并注册活页夹

    protected void Application_Start() { ModelBinders.Binders.DefaultBinder = new EmptyStringModelBinder(); RegisterRoutes( RouteTable.Routes ); }

    【讨论】:

      猜你喜欢
      • 2012-04-23
      • 2011-08-23
      • 2011-11-17
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多