Asp.net mvc 3 如何在@Html.Partial中传递嵌套模型值
我在里面有一个模型,我还有一个对象,我正试图在@Html.Partial中使用它 代码片段Asp.net mvc 3 如何在@Html.Partial中传递嵌套模型值,asp.net-mvc-3,Asp.net Mvc 3,我在里面有一个模型,我还有一个对象,我正试图在@Html.Partial中使用它 代码片段 public class Order{ public string Id{set;get;} public Address BillingAdress{set;get;} public Address ShippingAddress{set;get;} } public class Address{ public int Id{set;get;} public str
public class Order{
public string Id{set;get;}
public Address BillingAdress{set;get;}
public Address ShippingAddress{set;get;}
}
public class Address{
public int Id{set;get;}
public string Address{set;get;}
}
鉴于
@model Order
OrderId:
@Html.TextBoxFor(x=>Model.Id)
ShippingAdress:
@Html.Partial("Adress", Model.ShippingAdress)
BillingAddress:
@Html.Partial("Adress", Model.BillingAdress)
这是行不通的。
但是当我传递模型而不是Model.shippingaddress和Model.billingaddress时,TryUpdateModel(Order)在控制器动作中工作,有人能告诉我为什么吗??
我在网上搜索过,但没有得到任何concreete解决方案,所以请帮助我?原因是该部分不遵守输入字段的命名约定。改用编辑器模板:
@model Order
OrderId:
@Html.TextBoxFor(x => x.Id)
ShippingAdress:
@Html.EditorFor(x => x.ShippingAdress)
BillingAddress:
@Html.EditorFor(x => x.BillingAdress)
现在将您的Address.cshtml
移动到~/Views/Shared/EditorTemplates/Address.cshtml
。模板的名称和位置很重要。它应该位于~/Views/Shared/EditorTemplates
中,并以与模型类型相同的方式命名(Address.cshtml
):
@model Address
...