问题 DefaultModelBinder不绑定嵌套模型


看起来其他人有这个问题,但我似乎无法找到解决方案。

我有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始终为null。

我的帖子看起来像这样:

“NAME = statichippo&BillingInfo.BillingName = statichippo”

为什么BillingInfo总是为空?


9979
2017-10-13 21:20


起源

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


答案:


状态无重复。您的问题在其他地方,无法确定您从哪里获取信息。默认模型绑定器与嵌套类完美匹配。我已经无限次地使用它并且它始终有效。

模型:

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&BillingInfo.BillingName=statichippo 完全绑定在POST操作中。同样适用于GET。


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

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

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


6
2017-10-13 21:27



你是对的。结果我的HTML有一个问题,并输出: - hackerhasid
提前输入;) - “Name = statichippo&BillingInfo =&BillingInfo.BillingName = statichippo” - hackerhasid
嵌套类型没有绑定我遇到了同样的问题。事实证明我的HTML也有问题。我有2个单选按钮,其名称与我的视图模型上的属性名称相同。单选按钮值也会发布,因此默认的模型绑定器会混淆。 - René


我有这个问题,答案就是盯着我看了几个小时。我在这里包括它因为我正在寻找没有绑定的嵌套模型而且得出了这个答案。

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

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

8
2018-05-29 14:22



我重新检查,我的视图模型也缺乏{get;组; - 有某种盲目阻止我们看到这个! - niico


这对我有用。

我改变了这个:

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

1
2017-11-28 23:07



也为我工作过。谢谢你的解决方案。 - Sam


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

我有类似的问题。我花了很多时间意外地发现问题,我不应该使用'model'作为属性名称

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

0
2018-05-03 15:05





我遇到了同样的问题,该项目的前任开发人员已将该属性注册到私有的setter,因为他没有在回发中使用此viewmodel。像这样的东西:

public MyViewModel NestedModel { get; private set; }

改为:

public MyViewModel NestedModel { get; set; }

0
2018-01-25 09:24