如何使用 Html.Partial() 方法通过显式路径渲染部分视图
大家好,我正在使用 MVC 3 (Razor)。我有以下问题:
我有一些常见内容被分成部分视图。但我不需要将其放在默认位置(views/shared
或 views/controller-name
),而是需要将其放在不同的位置(views /shared/new-folder
或 view/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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用应用程序虚拟路径进行引用(请注意路径开头的
~\
):You need to reference using an application virtual path (notice the
~\
at the beginning of the path):如果您还配置了一个操作来返回该部分视图,您也可以这样做:
@{ 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.