ASP.NET MVC 控制器返回不在共享文件夹中的部分视图

发布于 2025-01-10 02:56:37 字数 1361 浏览 0 评论 0原文

我的控制器中有一个调用此部分视图的方法:

return PartialView("_tinzProdListPageTemplate", tblStoreLocation);

该部分视图位于 Shared 文件夹内,上面的代码工作正常并加载视图。但现在我想将 _tinzProdListPageTemplate 移动到 Shared 文件夹内名为 Templates 的另一个文件夹,以便我可以更好地组织我的项目。

我尝试过这些,但它们不起作用。似乎只有直接从默认的 Shared 文件夹调用时,才能从控制器加载部分视图。 (我什至尝试将 .cshtml 添加到末尾作为上面示例的扩展,但也不起作用)

return PartialView("Templates/_tinzProdListPageTemplate", tblStoreLocation);

return PartialView("Shared/Templates/_tinzProdListPageTemplate", tblStoreLocation);

return PartialView("~/Shared/Templates/_tinzProdListPageTemplate", tblStoreLocation);

那么如何返回不在 PartialView 内的 PartialView代码>共享文件夹。它位于 Shared 文件夹内的文件夹内?

我正在使用在 .NET Framework 上运行的 ASP.NET MVC 5。

这是我得到的错误:

未找到分部视图“_tinzProdListPageTemplate”或没有视图引擎支持搜索的位置。搜索了以下位置:

~/Views/Store/_tinzProdListPageTemplate.aspx
〜/Views/Store/_tinzProdListPageTemplate.ascx
〜/Views/Shared/_tinzProdListPageTemplate.aspx
〜/Views/Shared/_tinzProdListPageTemplate.ascx
〜/Views/Store/_tinzProdListPageTemplate.cshtml
〜/Views/Store/_tinzProdListPageTemplate.vbhtml
〜/Views/Shared/_tinzProdListPageTemplate.cshtml
〜/Views/Shared/_tinzProdListPageTemplate.vbhtml

I have this method inside my controller that calls this partial view:

return PartialView("_tinzProdListPageTemplate", tblStoreLocation);

That partial view is inside the Shared folder and the above code works fine and loads the view. But now I want to move _tinzProdListPageTemplate to another folder that is inside the Shared folder called Templates so that I can better organize my project.

I tried these but they don't work. It seems that only when calling straight from the default Shared folder, can you load a partial view from the controller. (I even tried adding .cshtml to the end as the extension on the examples above but did not work either)

return PartialView("Templates/_tinzProdListPageTemplate", tblStoreLocation);

return PartialView("Shared/Templates/_tinzProdListPageTemplate", tblStoreLocation);

return PartialView("~/Shared/Templates/_tinzProdListPageTemplate", tblStoreLocation);

So how do I return a PartialView that is not inside the Shared folder. Its inside a folder that is inside the Shared folder?

I am using ASP.NET MVC 5 running on the .NET Framework.

This is the error I get:

The partial view '_tinzProdListPageTemplate' was not found or no view engine supports the searched locations. The following locations were searched:

~/Views/Store/_tinzProdListPageTemplate.aspx
~/Views/Store/_tinzProdListPageTemplate.ascx
~/Views/Shared/_tinzProdListPageTemplate.aspx
~/Views/Shared/_tinzProdListPageTemplate.ascx
~/Views/Store/_tinzProdListPageTemplate.cshtml
~/Views/Store/_tinzProdListPageTemplate.vbhtml
~/Views/Shared/_tinzProdListPageTemplate.cshtml
~/Views/Shared/_tinzProdListPageTemplate.vbhtml

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

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

发布评论

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

评论(1

柠北森屋 2025-01-17 02:56:37

完整的视图名称如下所示

“~/Areas/Public/Views/Home/Index.cshtml”

我认为您只是忘记在末尾添加扩展名。

也许你想要一个像这里一样的 CustomViewEngine
在自定义视图引擎中使用子文件夹

如果您注册了自定义视图引擎,那么您可以设置所有搜索位置,例如

ViewLocation, PartialViewLocation, MasterLocation

A complete view name would look like this

"~/Areas/Public/Views/Home/Index.cshtml"

I think you only forgot to add the extension at the end.

Maybe you want a CustomViewEngine like here
Working with subfolders in custom view engine

If you register a custom view engine then you can set all the search locations like

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