将模型传递到局部视图 [英] Passing a model to a partial view

查看:78
本文介绍了将模型传递到局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个_Address部分视图.此局部视图包含与地址模型匹配的所有地址字段.在此视图的顶部,我像这样设置模型:
@model Data.Address

I have an _Address partial view. 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

在我的CustomerInfo视图中,我尝试以下方法将地址"字段呈现为表单的一部分:

In my CustomerInfo view I try the following to render the Address fields as part of my form:

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

我遇到的错误是:

传递到字典中的模型项的类型为"Data.Customer",但是此字典需要类型为"Data.Address"的模型项.

The model item passed into the dictionary is of type 'Data.Customer', but this dictionary requires a model item of type 'Data.Address'.

我假设我收到此错误,因为在CustomerInfo视图中声明的模型的类型为Data.Customer,并且它会自动尝试将其传递给我的_Address视图,该视图中的模型类型为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.

正确执行此操作的正确方法是什么?我注意到也有一个@ Html.RenderPartial("ViewName",model)帮助器,但是由于我的CustomerInfo视图中的主要模型是Data.Customer,所以不知道如何传递它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.

推荐答案

我猜您的主模型 Data.Customer 已经具有一个或多个类型为 Data.Address 您要显示的内容.因此,您将使用 @ Html.Partial 帮助器,并按如下所示传递此属性的值:

I guess that your primary model Data.Customer already has one or more properties of type Data.Address that you would like to display. So you would use the @Html.Partial helper and pass the value of this property like that:

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

作为使用 Html.Partial 帮助程序的替代方法,您可以使用编辑器/显示"模板.他们按惯例工作.因此,将您的 _Address.cshtml 部分重命名为〜/Views/Shared/EditorTemplates/Address.cshtml ,然后在主视图中使用:

As an alternative to using the Html.Partial helper you could use Editor/Display templates. They work by convention. So rename your _Address.cshtml partial to ~/Views/Shared/EditorTemplates/Address.cshtml and then inside your main view use:

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

或者如果部分不包含输入字段或表单,则可以使用显示模板:〜/Views/Shared/DisplayTemplates/Address.cshtml ,然后:

or if the partial doesn't contain input fields or forms you could use a display template: ~/Views/Shared/DisplayTemplates/Address.cshtml and then:

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

此处的约定是模板的名称和位置.由于 SomeAddressProperty 的类型为 Address ,因此ASP.NET MVC将查找相应的 Address.cshtml 模板.

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.

如果主视图模型没有address属性,则应继续添加一个或多个这样的属性,并在呈现主视图的控制器操作中填充它们.然后,您可以为每个属性调用局部函数.

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.

这篇关于将模型传递到局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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