如何在@ Html.Partial通过嵌套模型值 [英] How to pass a nested model value in @Html.Partial

查看:404
本文介绍了如何在@ Html.Partial通过嵌套模型值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型里面为了我有我试图在@ Html.Partial使用另一个对象
code段

I have a model as order inside it I have a another object which I am trying to use in @Html.Partial Code snippet

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

在View

  @model Order
  OrderId: 
  @Html.TextBoxFor(x=>Model.Id)

  ShippingAdress: 
  @Html.Partial("Adress", Model.ShippingAdress)

  BillingAddress:
  @Html.Partial("Adress", Model.BillingAdress)

这是行不通的。
但是当我传递模型来代替Model.ShippingAdress和Model.BillingAdress,TryUpdateModel(顺序)在控制器动作的工作任何一个可以告诉我为什么?
我寻觅净,但没有得到任何concreete解决方案,因此,请帮助我?

this is not working . but when I am passing Model instead of Model.ShippingAdress and Model.BillingAdress, TryUpdateModel(Order) is working in controller action can any one tell me why?? I have searched in net but not got any concreete solution so please help me?

推荐答案

这其中的原因是由于部分是不尊重的输入域的命名约定。使用编辑模板,而不是:

The reason for that is because the Partial is not respecting the naming convention for the input fields. Use an editor template instead:

@model Order
OrderId: 
@Html.TextBoxFor(x => x.Id)

ShippingAdress: 
@Html.EditorFor(x => x.ShippingAdress)

BillingAddress:
@Html.EditorFor(x => x.BillingAdress)

现在将你的 Adress.cshtml 〜/查看/共享/ EditorTemplates / Address.cshtml 。模板的名称和位置是重要的。它应位于〜/查看/共享/ EditorTemplates 键,命名方式为模型的类型相同( Address.cshtml

Now move your Adress.cshtml to ~/Views/Shared/EditorTemplates/Address.cshtml. The name and location of the template is important. It should be located in ~/Views/Shared/EditorTemplates and named the same way as the model type (Address.cshtml):

@model Address
...

这篇关于如何在@ Html.Partial通过嵌套模型值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆