如何在 ASP.NET MVC 中的当前区域之外使用 EditorTemplates

发布于 2024-09-13 22:11:41 字数 693 浏览 7 评论 0原文

考虑一个具有 2 个区域的项目。

/区域/博客 /Areas/Dashboard

现在假设我的博客区域有一个 SpecialBlog 类型的编辑器。 /Areas/Blog/Views/Blog/EditorTemplates/SpecialBlog.ascx

如果属于我的仪表板区域的视图想要显示特殊博客怎么办?

以下代码适用于“博客”区域内的视图,但不适用于“仪表板”区域。

Html.EditorFor (model => model)  // model is type SpecialBlog

即使提供路径失败,

Html.EditorFor (model => model, "~/Areas/Blog/Views/Blog/EditorTemplates/SpecialBlog.ascx")

我可以工作的一件事是

Html.RenderPartial (Model, "~/Areas/Blog/Views/Blog/EditorTemplates/SpecialBlog.ascx");

但是 SpecialBlog 内的任何路由都会失败。 (即它有自己的 Html.EditorFor 调用博客区域中的其他编辑器模板)。

我是否在做一些根本性错误的事情?

Consider a project with 2 Areas.

/Areas/Blog
/Areas/Dashboard

Now say that my Blog area has an editor for the type SpecialBlog. /Areas/Blog/Views/Blog/EditorTemplates/SpecialBlog.ascx

What if a view that is part of my Dashboard Area would like to display a special blog?

The following code works from Views inside the "Blog" Area but not from the "Dashboard" Area.

Html.EditorFor (model => model)  // model is type SpecialBlog

Even providing the path fails,

Html.EditorFor (model => model, "~/Areas/Blog/Views/Blog/EditorTemplates/SpecialBlog.ascx")

The one thing I can get working is

Html.RenderPartial (Model, "~/Areas/Blog/Views/Blog/EditorTemplates/SpecialBlog.ascx");

But any routing within the SpecialBlog then fails. (ie. it has its own Html.EditorFor calls to other editor templates in the Blog Area).

Am I doing something fundamentally wrong?

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

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

发布评论

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

评论(2

演出会有结束 2024-09-20 22:11:41

我相信将其放在共享文件夹中会起作用。

如果您不想放入主 /shared 文件夹,您可以尝试在 ViewEngine 中设置另一条路径(在 Application_start 处添加,或从您正在使用的路径派生)到 /areas/shared 文件夹(此意味着为所有区域创建文件夹并将模板放在那里。

I believe that putting this in the Shared folder will work.

If you don't want to put in the main /shared folder, you could try setting up another path in your ViewEngine (add at Application_start, or derive from the one you're using) to something like /areas/shared folder (this means, creating the folder) for all the areas and putting the templates there.

稳稳的幸福 2024-09-20 22:11:41

我遇到了同样的问题,我想使用下面的

~/Areas/ActivityPicker/Views/ActivityPicker/EditorTemplates/ActivityPicker.cshtml

这远非福音,但我认为你不能做到这一点(在 MVC 5 中)。当我尝试并调试它时,它最终出现在下面。

“EditorTemplates/~/Areas/ActivityPicker/Views/ActivityPicker/EditorTemplates/ActivityPicker.cshtml”

,即它在开头添加了 EditorTemplates。

然后它会检查通常的位置来找到它,即视图和直接区域的视图+编辑器模板,因为它不以“~”开头,所以它不会检查所有位置,只会检查公共视图和当前区域(检查 VirtualPathProviderViewEnginer::IsSpecificPath)

还将 '~' 替换为 '../' 也没有帮助,即您不能执行如下操作:

“EditorTemplates/../../../Areas/ActivityPicker/Views /ActivityPicker/EditorTemplates/ActivityPicker.cshtml”

出于同样的原因

即使这有效,我也不想这样做,因为那样你就会回复可能在后续版本中中断的副作用

所以,总而言之,我不'目前不认为你可以,很高兴被告知否则,因为这对我来说似乎是一个限制。我想要页面上同一视图的多个实例,建议使用编辑器模板,但您不能在不同区域之间使用它们

I had the same problem, I wanted to use the below

~/Areas/ActivityPicker/Views/ActivityPicker/EditorTemplates/ActivityPicker.cshtml

This is far from gospel but I don't think you can do this (In MVC 5). When I tried and debugged through it it ended up at the below.

"EditorTemplates/~/Areas/ActivityPicker/Views/ActivityPicker/EditorTemplates/ActivityPicker.cshtml"

i.e. it added EditorTemplates to the beginning.

It then looked through the usual places to find this, i.e. the View and the immediate Area's Views + the Editor Templates, because it doesn't start with '~' it won't check all locations, only the common View and the current Area (check in VirtualPathProviderViewEnginer::IsSpecificPath)

Also replacing '~' with '../' doesnt help, i.e. you can't do something like the below:

"EditorTemplates/../../../Areas/ActivityPicker/Views/ActivityPicker/EditorTemplates/ActivityPicker.cshtml"

for the same reason

Even if this worked I wouldn't want to do it as then you'll be replying on a side effect which could break in a subsequent release

So, in summary I don't think you can at present, would be happy to be told otherwise as it seems like a limitation to me. I want multiple instances of the same View on a page, Editor Templates are the advised route for this but you can't use them between different areas

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