当 ModelState 在 HttpPost 上无效时,ASP.NET MVC 自动绑定刷新的模型

发布于 2024-09-29 20:59:54 字数 1449 浏览 2 评论 0原文

我正在开发 ASP.NET MVC2 应用程序。我开始意识到 MVC 在幕后所做的一件非常令人惊讶但又令人惊奇的事情,与 ModelState 和模型绑定有关。我有一个 ViewModel,其中包含大量数据 - 一些字段是表单的一部分,而其他字段只是 UI 的一部分。在 HttpPost 上,我的 Action 方法使用 DefaultModelBinder,它尝试绑定整个模型,但只有表单一部分的字段被成功反序列化 - 所有其他字段都保持为空。这很好并且可以理解。如果 ModelState 无效,我需要从数据库刷新模型并绑定这些特定的表单字段,然后再返回到同一编辑视图以显示那些关联的 ModelState 验证错误。

这就是我的惊讶和好奇的地方。我的假设是,为了将表单字段与刷新的模型绑定,我需要调用 UpdateModel()TryUpdateModel< >(),传入新刷新的模型。例如:

[HttpPost]
public ActionResult EditDetail(EditDetailItemModel model)
{
    if (model.IsValid)
    {
        // Save the results to the db

        return RedirectToAction(...)
    }

    // Can't simply "return View(model)". Not all fields in EditDetailItemModel
    // were part of the form - thus they returned null. Have to refresh
    // model from the db.

    var refreshedModel = RefreshModelFromDB();

    // Is this line necessary?????
    TryUpdateModel<EditDetailItemModel>(refreshedModel);

    return View(refreshedModel);
}

但是,我发现如果我只是将 refreshedModel 返回到视图而不调用 TryUpdateModel<>()刷新的模型已自动与发布的表单字段值绑定!!因此,这里不需要 TryUpdateModel<>()

我能理解它的唯一方法是,由于 ModelState 处于无效状态,一旦我使用刷新的模型返回视图,“MVC 渲染引擎”就会循环遍历 ModelState 错误,并将这些属性值与刷新的模型绑定在一起。模型。这简直太棒了!但是,我想要证明这个假设的证据。我在网络上的任何地方都找不到与此相关的文档。任何人都可以证实我对为什么/如何发生这种令人敬畏的自动绑定行为的假设和/或教育我为什么/如何发生它,希望有一些在线文档链接支持,以便我更全面地了解幕后发生的事情?

I'm working on an ASP.NET MVC2 app. I've come to realize a very surprising, yet amazing thing that MVC does behind the scenes having to do with the ModelState and model binding. I have a ViewModel which has a whole bunch of data - some fields being part of a form while others are simply part of the UI. On HttpPost, my Action method uses the DefaultModelBinder which attempts to bind the whole model, but only fields which were part of the form are successfully deserialized - all others remain null. That's fine and understandable. If the ModelState is invalid, I need to refresh the model from the db and bind those particular form fields before returning to the same edit view to display those associated ModelState validation errors.

Here's where my amazement and curiosity comes. It was my assumption that in order for me to bind the form fields with the refreshed model, I needed to make a call to either UpdateModel() or TryUpdateModel<>(), passing in the newly refreshed model. For example:

[HttpPost]
public ActionResult EditDetail(EditDetailItemModel model)
{
    if (model.IsValid)
    {
        // Save the results to the db

        return RedirectToAction(...)
    }

    // Can't simply "return View(model)". Not all fields in EditDetailItemModel
    // were part of the form - thus they returned null. Have to refresh
    // model from the db.

    var refreshedModel = RefreshModelFromDB();

    // Is this line necessary?????
    TryUpdateModel<EditDetailItemModel>(refreshedModel);

    return View(refreshedModel);
}

But, what I found was that if I simply returned refreshedModel to the view WITHOUT making a call to TryUpdateModel<>(), the refreshed model was automatically bound with the form field values posted!! Hence, the TryUpdateModel<>() is not needed here!

The only way I can make any sense of it is that since the ModelState is in an invalid state, once I returned the view with the refreshed model, the "MVC rendering engine" looped through the ModelState errors and bound those property values with my refreshed model. That is simply AWESOME! But, I want proof as to this assumption. I can't find documentation regarding this anywhere on the web. Can anyone either confirm my hypothesis of WHY/HOW this AWESOME auto binding behavior is occuring and/or educate me as to why/how it's happening, hopefully backed up with some online documentation links so I understand more fully what's going on under the covers?

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

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

发布评论

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

评论(1

天涯离梦残月幽梦 2024-10-06 20:59:54
public ActionResult EditDetail(EditDetailItemModel model)

该行将执行模型绑定。将 ActionMethod 参数视为始终通过调用 UpdateModel 来填充。

您在视图中看不到refreshedModel 的值,而是看到EditDetailItemModel 中的ModelState 条目和值。

public ActionResult EditDetail(EditDetailItemModel model)

That line will perform model binding. Think of ActionMethod parameters as always being populated by a call to UpdateModel.

You are not seeing refreshedModel's values in the view, you are seeing the ModelState entries and values from EditDetailItemModel.

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