DefaultModelBinder 不绑定嵌套模型

发布于 2024-09-27 12:16:53 字数 606 浏览 1 评论 0原文

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

我有 2 个模型:人和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 总是为空?

Looks like others have had this problem but I can't seem to find a solution.

I have 2 Models: Person & BillingInfo:

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

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

And I'm trying to bind this straight into my Action using the DefaultModelBinder.

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

However, while the Person.Name property is set, the BillingInfo is always null.

My post looks like this:

"Name=statichippo&BillingInfo.BillingName=statichippo"

Why is BillingInfo always null?

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

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

发布评论

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

评论(5

花期渐远 2024-10-04 12:16:53

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

确保嵌套模型的属性(与您希望绑定起作用的任何模型一样)具有正确的访问器。

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

I had this problem, and the answer was staring me in the face for a few hours. I'm including it here because I was searching for nested models not binding and came to this answer.

Make sure that your nested model's properties, like any of your models that you want the binding to work for, have the correct accessors.

    // 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; }
左耳近心 2024-10-04 12:16:53

状态无重现。您的问题在其他地方,无法从您提供的信息中确定出在哪里。默认模型绑定器与嵌套类完美配合。我已经使用它无数次了,而且一直有效。

模型:

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

请注意操作参数如何称为 billingInfo,其名称与 BillingInfo 属性相同。确保这不是您的情况。

Status no repro. Your problem is elsewhere and unable to determine where from what you've given as information. The default model binder works perfectly fine with nested classes. I've used it an infinity of times and it has always worked.

Model:

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

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

Controller:

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

View:

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

Posted values: Name=statichippo&BillingInfo.BillingName=statichippo is perfectly bound in the POST action. Same works with GET as well.


One possible case when this might not work is the following:

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

Notice how the action parameter is called billingInfo, same name as the BillingInfo property. Make sure this is not your case.

油饼 2024-10-04 12:16:53

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

public MyViewModel NestedModel { get; private set; }

改成这样:

public MyViewModel NestedModel { get; set; }

I had the same issue, the previous developer on the project had the property registered with a private setter as he wasn't using this viewmodel in a postback. Something like this:

public MyViewModel NestedModel { get; private set; }

changed to this:

public MyViewModel NestedModel { get; set; }
单调的奢华 2024-10-04 12:16:53

这对我有用。

我将其更改

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

This is what worked for me.

I changed this:

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

To:

[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);
    }
我一向站在原地 2024-10-04 12:16:53
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
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; }
}

I have had the similiar problem. I spent many hours and find the problem accidentally that I should not use 'model' for the property name

@Html.TextBoxFor(m => m.xmodel.Email) //This is OK
@Html.TextBoxFor(m => m.model.Email) //This is not OK
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文