问题描述
看起来像其他人遇到了这个问题,但我似乎无法找到解决的办法。
Looks like others have had this problem but I can't seem to find a solution.
我有2个型号:人与放大器; BillingInfo:
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; }
}
和我试图用DefaultModelBinder这个绑定直入我的动作。
And I'm trying to bind this straight into my Action using the DefaultModelBinder.
public ActionResult DoStuff(Person model)
{
// do stuff
}
然而,当Person.Name属性设置,在BillingInfo始终为null。
However, while the Person.Name property is set, the BillingInfo is always null.
我的帖子是这样的:
NAME = statichippo&安培; BillingInfo.BillingName = statichippo
为什么总是BillingInfo空?
Why is BillingInfo always null?
推荐答案
状态没有摄制。你的问题是其他地方无法确定从你给什么样的信息在哪里。默认的模型粘合剂适用于嵌套类完全没有问题。我用它的时候无边,它一直工作。
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.
型号:
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" />
<% } %>
发布的值:名称= statichippo&安培; BillingInfo.BillingName = statichippo
在POST操作是完全约束。同一作品以获得尽可能好。
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();
}
注意操作参数是如何被调用 billingInfo
,相同的名称 BillingInfo
属性。确保这是不是你的情况。
Notice how the action parameter is called billingInfo
, same name as the BillingInfo
property. Make sure this is not your case.
这篇关于DefaultModelBinder没有约束力嵌套模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!