0

このメソッドに入ってきた arcm.Notes が null だったため、UpdateModel は失敗し、空の文字列にしたいのです。

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" });
4

1 に答える 1

1

これは、MVC2 以降の既定の動作だと思います。ここで 1 つの回避策に注意してください。

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 );
}

于 2011-11-01T02:20:11.947 に答える