1

我的问题是我有一个 MVC4 项目,我想创建一些组件,我想使用部分视图在某些视图中添加这些组件。我将向您展示我想在我的 MVC 项目中创建的文件夹逻辑的图片。

在此处输入图像描述

从这里我如何添加这个部分视图,因为当我尝试访问它们时,它们首先在我的应用程序的主 Views 文件夹中查看,有没有办法为我的应用程序创建自定义路由?

@{ Html.RenderPartial("Components\\Chart\\View\\Index.cshtml");  }

这给出了这样的错误。

未找到部分视图“组件\图表\视图\索引.cshtml”或没有视图引擎支持搜索的位置。搜索了以下位置:~/Views/Home/Conmponents\Chart\View\Index.cshtml.aspx ~/Views/Home/Conmponents\Chart\View\Index.cshtml.ascx ~/Views/Shared/Conmponents\Chart\ View\Index.cshtml.aspx ~/Views/Shared/Conmponents\Chart\View\Index.cshtml.ascx ~/Views/Home/Conmponents\Chart\View\Index.cshtml.cshtml ~/Views/Home/Conmponents\Chart \View\Index.cshtml.vbhtml ~/Views/Shared/Conmponents\Chart\View\Index.cshtml.cshtml ~/Views/Shared/Conmponents\Chart\View\Index.cshtml.vbhtml

4

1 回答 1

1

您可以指定使用~作为项目目录的路径。

Html.RenderPartial("~/Components/Chart/View/Index.cshtml",model)

这个问题也与路由无关。路由是将传入请求映射到您的操作方法。ASP.NET MVC 尝试使用控制器和操作名称来定位您的视图。如果它不存在,它还会检查共享文件夹。您可以通过指定确切路径来覆盖此行为,如上例所示。

更新:

您还必须对 View 文件夹进行一些配置。将您的 Web.config 复制到默认 Views 文件夹中或将所有视图配置移动到项目的 Web.config 文件应该可以解决问题。

于 2012-11-03T14:23:04.067 回答