如何在asp.net mvc 3中呈现局部视图? [英] How can I render Partial views in asp.net mvc 3?
问题描述
我在 ViewData.Model
中有一些数据,在我的视图中,我想编写局部视图并传递我在页面中拥有的当前模型.
I have some data in ViewData.Model
, and in my views I want to write a partial view and to pass their current model I have in my page.
如何传递他们当前的 ViewData.Model
并通过局部的位置渲染它们?
How I can pass their current ViewData.Model
and render them through the location of partials?
推荐答案
创建部分视图,例如:
@model YourModelType
<div>
<!-- HTML to render your object -->
</div>
然后在您的视图中使用:
Then in your view use:
@Html.Partial("YourPartialViewName", Model)
如果您不想使用强类型的局部视图,请从局部视图的顶部删除 @model YourModelType
,它将默认为 dynamic
类型.
If you do not want a strongly typed partial view remove the @model YourModelType
from the top of the partial view and it will default to a dynamic
type.
更新
默认视图引擎将在与调用局部视图的视图相同的文件夹中搜索局部视图,然后在〜/Views/Shared文件夹中搜索局部视图.如果您的局部文件位于其他文件夹中,则需要使用完整路径.请注意在下面的路径中使用〜/
.
The default view engine will search for partial views in the same folder as the view calling the partial and then in the ~/Views/Shared folder. If your partial is located in a different folder then you need to use the full path. Note the use of ~/
in the path below.
@Html.Partial("~/Views/Partials/SeachResult.cshtml", Model)
这篇关于如何在asp.net mvc 3中呈现局部视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!