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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:02:38  来源:igfitidea点击:

how i can render Partial views in asp.net mvc 3

asp.net-mvcasp.net-mvc-3partial-viewspartial

提问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.Modeland 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 YourModelTypefrom the top of the partial view and it will default to a dynamictype.

如果你不想要一个强类型的局部视图,@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) %>