如何使用 Html.Partial() 方法通过显式路径渲染部分视图

发布于 2024-12-08 21:34:02 字数 480 浏览 1 评论 0原文

大家好,我正在使用 MVC 3 (Razor)。我有以下问题:

我有一些常见内容被分成部分视图。但我不需要将其放在默认位置(views/sharedviews/controller-name),而是需要将其放在不同的位置(views /shared/new-folderview/controller-name/new-folder)。

我尝试了这个: @Html.Partial("views/shared/new-folder/partial-view-name") 甚至 @Html.Partial("views/shared/new-folder /partial-view-name.cshtml"),但似乎MVC3只将参数视为视图名称,并且完全忽略了任何路径信息。

也许我做错了什么,有人可以帮助我吗?:) 非常感谢!

Everyone, I'm using MVC 3 (Razor). I have the following problem:

I have some common contents segregated into a partial view. But rather than to put it in the default location (views/shared or views/controller-name), I need to put it in a different location (views/shared/new-folder or view/controller-name/new-folder).

I tried this : @Html.Partial("views/shared/new-folder/partial-view-name") or even @Html.Partial("views/shared/new-folder/partial-view-name.cshtml"),but it seems that MVC3 only consider the parameter as a view name, and it totally ignored any path information.

Maybe I did something wrong ,can anybody help me with this ?:) Thank you very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

堇年纸鸢 2024-12-15 21:34:02

您需要使用应用程序虚拟路径进行引用(请注意路径开头的 ~\):

@Html.Partial("~\\views\\shared\\new-folder\\partial-view-name.cshtml")

You need to reference using an application virtual path (notice the ~\ at the beginning of the path):

@Html.Partial("~\\views\\shared\\new-folder\\partial-view-name.cshtml")
暖心男生 2024-12-15 21:34:02

如果您还配置了一个操作来返回该部分视图,您也可以这样做:

@{ Html.RenderAction("PartialViewAction", "PartialViewCOntroller");}

这可能会更好,因为您不应该这样做在代码中对视图的引用进行硬编码。在不同的服务器上部署硬编码引用可能会破坏应用程序,但调用操作来返回视图则不会。

If you have also configured an action to return that Partial View, You could also do:

@{ Html.RenderAction("PartialViewAction", "PartialViewCOntroller");}

This is probably better since you shouldn't be hard coding references to Views in your code. Deploying a hard coded reference on a different server could break the application, but calling an action to return a view won't.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文