mvc3 - 在不同区域使用部分视图
我有两个关于部分视图的问题...
何时使用部分视图与 @helper 方法,我都使用过 可以互换,并且希望在他们的 用法。你们是做什么的?
如何引用另一个区域的局部视图。
我有一个名为 admin 的区域,并且在常规 Views 目录中有部分视图。我如何使用它..我已经尝试了以下方法,但由于找不到它而不起作用。
@Html.Partial(VirtualPathUtility.ToAbsolute("~/Views/ControllerName/_PartialView"),
Model)
其他我已经尝试过 -
@Html.Partial("~/Views/ControllerName/_PartialView", Model)
I have two questions regarding partial views...
When to use Partial views vs @helper methods, i have used both
interchangeably and would like to get more consistent in their
usage. What do you guys do?How do you reference a partial view from another area.
I have an area called admin and i have a partial view in the regular Views directory. How do i use it .. i have tried the following which dont work as it cant be found.
@Html.Partial(VirtualPathUtility.ToAbsolute("~/Views/ControllerName/_PartialView"),
Model)
other i have tried -
@Html.Partial("~/Views/ControllerName/_PartialView", Model)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当你说“助手”时,我不确定你指的是 Html 助手还是剃刀助手。无论如何,我只在它是像控件这样的小型单独项目时创建 Html 助手。
如果您指的是 Razor 助手,那么它们与 Partials 不同,因为您可以像函数一样调用它们,传递您想要的任何参数。部分很大程度上取决于“模型”系统(当然还有 Temp/ViewData/Bag。
这完全取决于您希望如何使用代码。
至于您的部分。您必须包含后缀。
I'm not sure if you mean Html helpers, or razor helpers when you say "helpers" In any case, I only create Html helpers when it's a small, idividual item like a control.
If you mean Razor helpers, then they are different from Partials in that you can call them like functions, passing whatever parameters you want. Partials are largely stuck with the "model" system (and of course Temp/ViewData/Bag.
It's all about how you want to work with the code.
As for your Partial. You have to include the suffix.
由于提问者询问了区域,因此这里介绍了如何在区域中进行操作
Since the questioner asked about areas here's how to do it in an area
我只是给出我正在尝试做的事情的具体而简单的例子。
我需要能够使用位于主共享文件夹中的部分视图从区域页面注销。这就是我所做的:
在区域视图中,我通过以下方式引用部分视图
在 _LoginPartial 代码所在的主共享文件夹中 我添加了
{new = area (" ")}
,来自:至:
Hope这在某种程度上有帮助!
I am just giving specific and simple example of what I'm trying to do.
I need to be able to logoff from an area page using the partialview located in the main shared folder. Here's what I did:
At the area view I reference the partial view by
At the Main shared folder where the _LoginPartial code was located I added
{new = area ("")}
, from:to:
Hope that helps in some way!
另一种选择是使您想要在共享区域之间共享的部分视图。
所以你把它放在主 ~/Views/Shared/ 文件夹中,例如
你可以通过说从任何区域引用它
Another option is to make the partial view you want to share between areas SHARED.
So you put it in the main ~/Views/Shared/ folder, e.g.
You can then refer to it from any area by saying
确保区域中的控制器具有
[Area("MyArea")]
注释。截至这篇文章,通过 ASP.NET Core 中的 Ajax div 更新从跨区域边界拉取部分视图对我来说适用于标签帮助器和 @Html.ActionLink。Make sure your Controllers in Areas have the
[Area("MyArea")]
annotation. As of this post, pulling in Partial Views from across Area boundries via Ajax div updates in ASP.NET Core works for me with Tag Helpers and @Html.ActionLink.