2010-10-13 77 views
7

看起来像其他人有这个问题,但我似乎无法找到解决方案。DefaultModelBinder不绑定嵌套模型

我有2种型号:人& BillingInfo:

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

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

我试图把它绑定直入使用DefaultModelBinder我的行动。

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

然而,当Person.Name属性设置,该BillingInfo始终为空。

我的帖子是这样的:

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

为什么BillingInfo总是空?

回答

5

状态no repro。你的问题在其他地方,无法确定你所提供的信息。默认的模型绑定器对嵌套类完全正常工作。我用它无限的时间,它一直工作。

型号:

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财产。确保这不是你的情况。

+0

你说得对。原来我的HTML有一个问题,并输出: – hackerhasid 2010-10-13 21:56:09

+0

过早输入;) - “名称= statichippo&BillingInfo =&BillingInfo.BillingName = statichippo” – hackerhasid 2010-10-13 21:56:35

+0

我有同样的问题,没有被绑定的嵌套类型。原来我的HTML也有问题。我有2个单选按钮,其名称与我的视图模型上的属性名称相同。单选按钮值也被发布,所以默认的模型绑定器会感到困惑。 – 2011-04-20 10:02:31

0
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 
6

我有这个问题,答案是盯着我的脸了几个小时。我在这里包括它,因为我正在寻找嵌套模型没有约束力并来到这个答案。

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

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

我重新检查,我的viewmodels也缺乏{get;组; } - 有一种盲目性阻止我们看到这个! – niico 2016-09-19 07:59:52

0

我有同样的问题,该项目的原开发人员已经为他没有使用该视图模型在回发带私人二传手注册的属性。事情是这样的:

public MyViewModel NestedModel { get; private set; } 

改成这样:

public MyViewModel NestedModel { get; set; } 
1

这是对我工作。

我改变了这个:

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

也为我工作。感谢您的解决方案。 – Sam 2018-02-15 17:52:11