asp.net-mvc RenderPartial 来自另一个控制器(和另一个文件夹中)的视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2879733/
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 00:19:38  来源:igfitidea点击:

RenderPartial a view from another controller (and in another folder)

asp.net-mvcrenderpartial

提问by George Silva

I two database entities that i need to represent and i need to output them in a single page.

我需要表示两个数据库实体,我需要在单个页面中输出它们。

I have something like this

我有这样的事情

Views Def ViewA ViewB Test ViewC

视图定义视图A 视图B 测试视图C

I want to ViewC to display ViewA, which displays ViewB.

我想ViewC显示ViewA,它显示ViewB。

Right now i'm using something like this:

现在我正在使用这样的东西:

// View C
<!-- bla -->
<% Html.RenderPartial(Url.Content("../Definition/DefinitionDetails"), i); %>


// View A
<!-- bla -->
<% Html.RenderPartial(Url.Content("../Definition/DefinitionEditActions")); %>

Is there a better to do this? I find that linking with relative pathnames can burn you. Any tips?

有没有更好的方法来做到这一点?我发现与相对路径名的链接可能会让您感到不适。有小费吗?

Any chance I can make somehtiing like...

任何机会我都可以做一些像...

Html.RenderPartial("Definition","DefinitionDetails",i); ?

Html.RenderPartial("Definition","DefinitionDetails",i); ?

Thanks for the help

谢谢您的帮助

回答by Ravinder Singh Bhanwar

this is works for me!

这对我有用!

@Html.Partial("~/Views/NewsFeeds/NewsFeedPartial.cshtml")

回答by Matt Sherman

You can refer to Views with full paths, like:

您可以参考具有完整路径的视图,例如:

Html.RenderPartial("~/Views/Definition/DefinitionDetails")

Even better, use the T4MVC library, which does the above and makes it (quasi-) strongly-typed. You can refer to any view from any controller or view. You use it like this:

更好的是,使用T4MVC 库,它执行上述操作并使其(准)强类型。您可以从任何控制器或视图引用任何视图。你像这样使用它:

Html.RenderPartial(MVC.Definition.Views.DefinitionDetails)

or

或者

Html.RenderPartial(MVC.Definition.Views.DefinitionDetails, myModel)

回答by Gone Coding

Just to clarify which options work exactly:

只是为了澄清哪些选项有效:

1) The extension of the view file isrequired if you supply a path.

1)如果您提供路径,需要视图文件的扩展名。

2) If you do not supply a path, do not supply the extension.

2) 如果您不提供路径,请不要提供扩展名。

The examples below assume cshtml files.

下面的示例假设 cshtml 文件。

Use RenderPartialin a code block:

使用RenderPartial一个代码块:

// This looks in default view folder, then shared, checking for .aspx, .cshtml etc
Html.RenderPartial("DefinitionDetails"); 

// This looks in specified path and requires the extension
Html.RenderPartial("~/Views/Definition/DefinitionDetails.cshtml");

Use Partialfor inline Razor syntax:

使用Partial内联剃刀语法:

// This looks in default view folder, then shared, checking for .aspx, .cshtml etc
@Html.Partial("DefinitionDetails")

// This looks in specified path and requires the extension
@Html.Partial("~/Views/Definition/DefinitionDetails.cshtml")

Note: Apparently RenderPartialis slightly faster than Partial, but I also expect fully pathed names will be a faster than letting MVC search for the file.

注意:显然RenderPartial比 略快Partial,但我也希望全路径名称比让 MVC 搜索文件更快。

If you are producing partials in a loop (i.e. from a collection in your view model), it is likely you will need to pass through specific viewmodels:

如果您在循环中生成部分(即从您的视图模型中的集合),您可能需要通过特定的视图模型:

e.g.

例如

   @foreach (var group in orderedGroups)
   {
       Html.RenderPartial("~/Views/ControllerName/ViewName.cshtml", group);
   }

I just had to do all this on a project and found the marked answer a little misleading.

我只需要在一个项目上完成所有这些,发现标记的答案有点误导。

回答by ridecar2

Could you not copy the partials into the shared folder then just do:

你能不能不将部分复制到共享文件夹然后做:

<% Html.RenderPartial("DefinitionDetails", i); %>and

<% Html.RenderPartial("DefinitionDetails", i); %>

<% Html.RenderPartial("DefinitionEditActions"); %>

<% Html.RenderPartial("DefinitionEditActions"); %>