asp.net-mvc 我如何在 asp.net mvc 3 中呈现部分视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5441615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how i can render Partial views in asp.net mvc 3
提问by David Glenn
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?
我如何通过它们的电流ViewData.Model
并通过部分的位置渲染它们?
回答by David Glenn
Create your partial view something like:
创建您的部分视图,例如:
@model YourModelType
<div>
<!-- HTML to render your object -->
</div>
Then in your view use:
然后在您看来使用:
@Html.Partial("YourPartialViewName", Model)
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.
如果你不想要一个强类型的局部视图,@model YourModelType
从局部视图的顶部删除,它会默认为一个dynamic
类型。
Update
更新
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.
默认视图引擎将在与调用局部视图的视图相同的文件夹中搜索局部视图,然后在 ~/Views/Shared 文件夹中。如果您的部分位于不同的文件夹中,那么您需要使用完整路径。请注意~/
以下路径中的使用。
@Html.Partial("~/Views/Partials/SeachResult.cshtml", Model)
回答by Kasper Holdum
<%= Html.Partial("PartialName", Model) %>