使用 T4MVC 指定部分视图路径

发布于 2024-08-11 08:53:11 字数 491 浏览 16 评论 0原文

我在 ASP.NET MVC 项目中使用 T4MVC。

我的观点是这样的:

<% Html.RenderPartial(MVC.SomeController.Views.PartialViewName); %>

以前是​​这样的:

<% Html.RenderPartial("../SomeController/PartialViewName"); %>

以前它工作正常,但是在我使用 T4MVC 指定部分视图之后,它无法找到该部分视图。

它只是试图在下面的路径中找到它,这是默认行为。

~/Views/SomeController
~/Views/Shared

有没有办法通过 T4MVC 指定位于其他控制器的视图文件夹中的部分视图?或者无论我在做什么,它是正确的吗?我缺少什么?

谢谢。

I am using T4MVC in our ASP.NET MVC project.

I have a statement like this in my view:

<% Html.RenderPartial(MVC.SomeController.Views.PartialViewName); %>

Which was previously like this:

<% Html.RenderPartial("../SomeController/PartialViewName"); %>

Previously it was working fine, but after I specified the partial view using T4MVC, its not able to locate that partial view.

Its Just trying to find it in the below paths, which is the default behaviour.

~/Views/SomeController
~/Views/Shared

Is there a way to specify a partial view which is in some other controller's views folder through T4MVC? or Whatever I am doing, is it correct ? What am I missing ?

Thanks.

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

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

发布评论

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

评论(2

爱的故事 2024-08-18 08:53:11

一种方法 - 您可以扩展视图引擎并使其了解特定的部分视图位置。我自己将所有部分视图放在 Views/Home/Partial (其中 Home=>controller name)文件夹中。

 public class ViewEngine : WebFormViewEngine
    {
        public ViewEngine()
        {
            PartialViewLocationFormats = PartialViewLocationFormats
                .Union(new[]
                       {
                           "~/Views/{1}/Partial/{0}.ascx",
                           "~/Views/Shared/Partial/{0}.ascx",
                       }).ToArray();
        }
    }

但听起来更多的是你的应用程序结构是错误的。控制器特定的部分视图不应呈现与另一个控制器绑定的部分视图。将这些部分视图放在共享文件夹中。

One approach - you can extend your viewengine and make it aware about specific partial view locations. I myself put all partial views in Views/Home/Partial (where Home=>controller name) folder.

 public class ViewEngine : WebFormViewEngine
    {
        public ViewEngine()
        {
            PartialViewLocationFormats = PartialViewLocationFormats
                .Union(new[]
                       {
                           "~/Views/{1}/Partial/{0}.ascx",
                           "~/Views/Shared/Partial/{0}.ascx",
                       }).ToArray();
        }
    }

But it sounds more that you are structuring your app wrong. Controller specific partial views should not render partial views that are tied with another controller. Put those partial views in shared folder.

梦罢 2024-08-18 08:53:11

您可能已经知道,我已更改 T4MVC 以生成视图的完整路径而不是短名称。所以上面的原始代码应该可以工作。如果您遇到问题,请告诉我。

这是版本 2.6.03。 下载页面

As you may already know, I have changed T4MVC to generate the full path to the view instead of the short name. So the original code you have above should just work. Let me know if you run into issues.

This is in build 2.6.03. Download Page.

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