简体   繁体   English

将模型传递到局部视图

[英]Passing a model to a partial view

I have an _Address partial view. 我有一个_Address局部视图。 This partial view contains all of the address fields that match the Address Model. 此部分视图包含与地址模型匹配的所有地址字段。 At the top of this view I set the model like this: 在这个视图的顶部,我设置了这样的模型:
@model Data.Address @model Data.Address

In my CustomerInfo view I try the following to render the Address fields as part of my form: 在我的CustomerInfo视图中,我尝试以下操作将地址字段呈现为表单的一部分:

@Html.Partial("~/Views/Shared/Partial/_Address.cshtml")

The error I am getting is: 我得到的错误是:

The model item passed into the dictionary is of type 'Data.Customer', but this dictionary requires a model item of type 'Data.Address'. 传递到字典中的模型项的类型为“Data.Customer”,但此字典需要“Data.Address”类型的模型项。

I am assuming I am getting this error because the model declared in my CustomerInfo view is of type Data.Customer and it is automatically trying to pass it to my _Address view which has a model of type Data.Address in it. 我假设我收到此错误,因为在我的CustomerInfo视图中声明的模型是Data.Customer类型,它会自动尝试将其传递给我的_Address视图,其中包含Data.Address类型的模型。

What is the proper way to make this flow properly? 正确使这种流动的正确方法是什么? I noticed there is also an @Html.RenderPartial("ViewName", model) helper but have no idea how to pass it Data.Address since the primary model in my CustomerInfo view is Data.Customer. 我注意到还有一个@ Html.RenderPartial(“ViewName”,模型)帮助器,但不知道如何将它传递给Data.Address,因为我的CustomerInfo视图中的主要模型是Data.Customer。

I guess that your primary model Data.Customer already has one or more properties of type Data.Address that you would like to display. 我猜你的主要模型Data.Customer已经有一个或多个你想要显示的Data.Address类型的属性。 So you would use the @Html.Partial helper and pass the value of this property like that: 所以你会使用@Html.Partial助手并传递这个属性的值,如下所示:

@Html.Partial("ViewName", Model.SomeAddressProperty)

As an alternative to using the Html.Partial helper you could use Editor/Display templates. 作为使用Html.Partial助手的替代方法,您可以使用编辑器/显示模板。 They work by convention. 他们按惯例工作。 So rename your _Address.cshtml partial to ~/Views/Shared/EditorTemplates/Address.cshtml and then inside your main view use: 所以将_Address.cshtml部分重命名为~/Views/Shared/EditorTemplates/Address.cshtml ,然后在主视图中使用:

@Html.EditorFor(x => x.SomeAddressProperty)

or if the partial doesn't contain input fields or forms you could use a display template: ~/Views/Shared/DisplayTemplates/Address.cshtml and then: 或者如果partial不包含输入字段或表单,则可以使用显示模板: ~/Views/Shared/DisplayTemplates/Address.cshtml然后:

@Html.DisplayFor(x => x.SomeAddressProperty)

The convention here is the name and location of the template. 这里的约定是模板的名称和位置。 Since the type of the SomeAddressProperty is Address , ASP.NET MVC will look for the corresponding Address.cshtml template. 由于SomeAddressProperty的类型是Address ,因此ASP.NET MVC将查找相应的Address.cshtml模板。

If your main view model doesn't have an address property you should simply go ahead and add one or more such properties and populate them in the controller action rendering the main view. 如果您的主视图模型没有地址属性,您应该继续添加一个或多个此类属性,并在呈现主视图的控制器操作中填充它们。 Then you can call the partial for each of those properties. 然后,您可以为每个属性调用partial。

First of all, don't pass Data.Customer to your main view, but only 'Data'. 首先,不要将Data.Customer传递给主视图,而只传递给“数据”。 This is more flexible, all your have to do then is fix the references inside the main view, so instead of @Model.FirstName , you use @Model.Customer.FirstName . 这是更灵活,所有需要做的则是固定主视图内引用,所以不是@Model.FirstName ,您使用@Model.Customer.FirstName This way, you can call your partial view with a more explicit @Html.Partial("~/Views/Shared/Partial/_Address.cshtml", @Model.Address) . 这样,您可以使用更明确的@Html.Partial("~/Views/Shared/Partial/_Address.cshtml", @Model.Address)调用部分视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM