MVC3 - UpdateModel...如何更改传入数据?

发布于 2024-12-13 04:17:19 字数 589 浏览 2 评论 0原文

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

UpdateModel fails because arcm.Notes was null coming into this method and I want it to be an empty string.

Maybe I need to refresh the ValueProvider (?) after setting Notes to "", so I can use 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" });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不可一世的女人 2024-12-20 04:17:19

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

http://brianreiter.org/2010/09/16/asp-net-mvc-2-0-undocumented-model-string-property-writing-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 );
}

This is the default behavior I believe since MVC2.
note one workaround here:

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

And register the binder


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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文