【问题标题】:DefaultModelBinder not binding nested modelDefaultModelBinder 不绑定嵌套模型
【发布时间】:2011-04-25 02:17:00
【问题描述】:

看起来其他人也遇到过这个问题,但我似乎找不到解决方案。

我有 2 个模型:Person 和 BillingInfo:

public class Person
{
 public string Name { get; set;}
 public BillingInfo BillingInfo { get; set; }
}

public class BillingInfo
{
 public string BillingName { get; set; }
}

我正在尝试使用 DefaultModelBinder 将其直接绑定到我的 Action 中。

public ActionResult DoStuff(Person model)
{
 // do stuff
}

但是,在设置 Person.Name 属性时,BillingInfo 始终为空。

我的帖子是这样的:

“名称=statichippo&BillingInfo.BillingName=statichippo”

为什么 BillingInfo 总是为空?

【问题讨论】:

  • 我遇到了同样的问题,在我的情况下,我错过了 ViewModel 中对象上的 Set{},而且我为同一属性定义了 Hidden Field 和 TextBoxFor 字段,两个值都被发布了,一个为空白(隐藏字段),另一个具有正确的用户输入值 (TextboxFor),但模型绑定始终将隐藏字段值置于用户输入值之上,因此它始终为空。
  • @Melu 谢谢!我也错过了属性设置器,并且无法找出嵌套对象无法绑定的原因。太糟糕了,这么多重构工具建议删除 ViewModel 上的 setter,其中嵌套对象是集合等。

标签: asp.net-mvc asp.net-mvc-2 viewmodel


【解决方案1】:

状态没有复制。您的问题出在其他地方,无法确定您提供的信息来自何处。默认模型绑定器与嵌套类完美配合。我已经用了无数次了,而且一直有效。

型号:

public class Person
{
    public string Name { get; set; }
    public BillingInfo BillingInfo { get; set; }
}

public class BillingInfo
{
    public string BillingName { get; set; }
}

控制器:

[HandleError]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new Person
        {
            Name = "statichippo",
            BillingInfo = new BillingInfo
            {
                BillingName = "statichippo"
            }
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(Person model)
    {
        return View(model);
    }
}

查看:

<% using (Html.BeginForm()) { %>
    Name: <%: Html.EditorFor(x => x.Name) %>
    <br/>
    BillingName: <%: Html.EditorFor(x => x.BillingInfo.BillingName) %>
    <input type="submit" value="OK" />
<% } %>

发布的值:Name=statichippo&amp;BillingInfo.BillingName=statichippo 完全绑定在 POST 操作中。同样适用于 GET。


这可能不起作用的一种可能情况如下:

public ActionResult Index(Person billingInfo)
{
    return View();
}

注意 action 参数如何被称为 billingInfo,与 BillingInfo 属性同名。确保这不是你的情况。

【讨论】:

  • 你是对的。原来我的 HTML 有问题并且正在输出:
  • 过早进入 ;) -- "Name=statichippo&BillingInfo=&BillingInfo.BillingName=statichippo"
  • 我遇到了同样的问题,嵌套类型没有被绑定。原来我的 HTML 也有问题。我有 2 个单选按钮,其名称与我的视图模型上的属性名称相同。单选按钮的值也会被发布,所以默认的模型绑定器会被混淆。
【解决方案2】:
public class MyNestedClass
{
    public string Email { get; set; }
}

public class LoginModel
{
//If you name the property as 'xmodel'(other than 'model' then it is working ok.
public MyNestedClass xmodel  {get; set;} 

//If you name the property as 'model', then is not working
public MyNestedClass model  {get; set;} 

public string Test { get; set; }
}

我遇到了类似的问题。我花了很多时间无意中发现了我不应该使用“模型”作为属性名称的问题

@Html.TextBoxFor(m => m.xmodel.Email) //This is OK
@Html.TextBoxFor(m => m.model.Email) //This is not OK

【讨论】:

    【解决方案3】:

    我遇到了这个问题,答案让我盯着我看了几个小时。我将它包含在这里是因为我正在搜索未绑定的嵌套模型并得出了这个答案。

    确保嵌套模型的属性(如您希望绑定适用的任何模型)具有正确的访问器。

        // Will not bind!
        public string Address1;
        public string Address2;
        public string Address3;
        public string Address4;
        public string Address5;
    
    
        // Will bind
        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string Address3 { get; set; }
        public string Address4 { get; set; }
        public string Address5 { get; set; }
    

    【讨论】:

    • 我重新检查了,我的视图模型也缺少 { get;放; } - 有某种失明阻止我们看到这个!
    【解决方案4】:

    我遇到了同样的问题,该项目的前一位开发人员将该属性注册到了一个私有设置器,因为他没有在回发中使用这个视图模型。像这样的:

    public MyViewModel NestedModel { get; private set; }
    

    改成这样:

    public MyViewModel NestedModel { get; set; }
    

    【讨论】:

      【解决方案5】:

      这对我有用。

      我改变了这个:

      [HttpPost]
          public ActionResult Index(Person model)
          {
              return View(model);
          }
      

      收件人:

      [HttpPost]
          public ActionResult Index(FormCollection fc)
          {
              Person model = new Person();
              model.BillingInfo.BillingName = fc["BillingInfo.BillingName"]
      
              /// Add more lines to complete all properties of model as necessary.
      
              return View(model);
          }
      

      【讨论】:

      • 也为我工作。感谢您的解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      • 2013-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多