需要帮助使用DefaultModelBinder嵌套模式 [英] Need help using the DefaultModelBinder for a nested model

查看:180
本文介绍了需要帮助使用DefaultModelBinder嵌套模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些相关的问题,但我无法找到问题的解答。

There are a few related questions, but I can't find an answer that works.

假设我有以下型号:

public class EditorViewModel
{
  public Account Account {get;set;}
  public string SomeSimpleStuff {get;set;}
}

public class Account
{
  public string AccountName {get;set;}
  public int MorePrimitivesFollow {get;set;}
}

和扩展视图的ViewPage< EditorViewModel> 其中执行以下操作:

and a view that extends ViewPage<EditorViewModel> which does the following:

<%= Html.TextBoxFor(model => model.Account.AccountName)%>
<%= Html.ValidationMessageFor(model => model.Account.AccountName)%>
<%= Html.TextBoxFor(model => model.SomeSimpleStuff )%>
<%= Html.ValidationMessageFor(model => model.SomeSimpleStuff )%>

和我的控制器是这样的:

and my controller looks like:

[HttpPost]
public virtual ActionResult Edit(EditorViewModel account)
{ /*...*/ }

我怎样才能得到正确绑定我EditorViewModel的DefaultModelBinder?如果没有做什么特别的事情,我让我的EditorViewModel的空实例的一切空或默认值。

How can I get the DefaultModelBinder to properly bind my EditorViewModel? Without doing anything special, I get an empty instance of my EditorViewModel with everything null or default.

我来最接近的是通过调用的UpdateModel 手动:

The closest I've come is by calling UpdateModel manually:

[HttpPost]
public virtual ActionResult Edit(EditorViewModel account)
{
    account.Account = new Account();
    UpdateModel(account.Account, "Account");
    // this kills me:
    UpdateModel(account);

这成功更新我的帐户属性模型,但是当我打电话的UpdateModel在帐户(让我EditorViewModel的公共属性的其余部分)我得到完全无益类型的模型......无法更新。没有内部异常,所以我无法弄清楚发生了什么事情错了。

This successfully updates my Account property model, but when I call UpdateModel on account (to get the rest of the public properties of my EditorViewModel) I get the completely unhelpful "The model of type ... could not be updated." There is no inner exception, so I can't figure out what's going wrong.

我应该怎样做呢?

推荐答案

该粘合剂感到困惑,因为它看到的参数动作方法被命名为帐户的,并且它看到进来的表单字段命名的 account.accountname 的,所以它在寻找你的EditorViewModel的帐户名属性。

The binder is getting confused because it sees that the parameter to your action method is named account, and it sees incoming form fields named account.accountname, so it's looking for an AccountName property on your EditorViewModel.

您可以通过重命名参数别的东西,不传入表单字段冲突解决这个问题,或者你可以坚持的[装订(preFIX =)]属性的参数。此属性说忽略的事实是,参数被命名的帐户的,和pretend我给了它一个空字符串名称,而不是。对粘合剂将寻找的 account.accountname 的而不是 account.account.accountname

You can fix this by renaming the parameter to something else that doesn't conflict with an incoming form field, or you can stick an [Bind(Prefix = "")] attribute on the parameter. This attribute says "ignore the fact that the parameter is named account, and pretend I had given it an empty string name instead." Then the binder will look for account.accountname instead of account.account.accountname.

编辑 - 进一步的信息:

在粘结剂看到一个名为复杂的参数的的,它看起来在任何东西当前请求指定的的*。所以,如果你的参数被命名的的,其类型有一个属性命名的的,传入的值预计将是的 foo.FirstName =约翰的例如。

When the binder sees a complex parameter named foo, it looks at the current request for anything named foo.*. So if your parameter were named foo and its type had a property named FirstName, the incoming value would be expected to be foo.FirstName=John, for example.

不过,如果粘合剂不看的的*作为请求的一部分,它只是查找*直接(不的的preFIX )。因此,只要没有一个的的*请求present,你可以提交的姓=约翰的和粘合剂会正确地理解这一点。但是,如果有任何的的*作为请求的一部分,在姓=约翰的值将不会在名字属性相匹配。

However, if the binder does not see a foo.* as part of the request, it just looks for the * directly (without the foo prefix). So as long as there wasn't a foo.* present in the request, you could submit FirstName=John and the binder would understand this correctly. But if there is any foo.* as part of the request, the FirstName=John value will not match the FirstName property.

您可以看到现在如何给参数动作方法相同的名称,它的属性会甩开这个逻辑之一。

You can see now how giving the parameter to your action method the same name as one of its properties would throw off this logic.

这篇关于需要帮助使用DefaultModelBinder嵌套模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆