返回特定文件夹中的部分视图

时间:2014-09-30 15:04:25

标签: asp.net-mvc-5 partial-views

我正在尝试组织我的MVC项目,更具体地说是项目中的视图和部分视图。但由于文件夹的位置,我必须给返回视图一个字符串路径。

我有一个名为ContactSearch.cshtml的简单视图

@{
    ViewBag.Title = "Contact Search";
}

<!-- Partial view - Search criteria -->
@{
    //Html.RenderPartial("_EnquiryBreadCrumb", "Enquiry");
    @Html.Action("_EnquiryBreadCrumb", "Enquiry")
}


<!-- Partial view - Search results grid -->
@{

}

查询控制器然后处理部分视图:

public class EnquiryController : Controller
    {
        #region Partials
        public ActionResult _EnquiryBreadCrumb()
        {
            return View("~/PartialViews/Enquiry/_EnquiryBreadCrumb.cshtml");
        }
        #endregion
    }

当我返回视图时,我收到此运行时错误:

The view at '~/PartialViews/Enquiry/_EnquiryBreadCrumb.cshtml' must derive from WebViewPage, or WebViewPage<TModel>.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The view at '~/PartialViews/Enquiry/_EnquiryBreadCrumb.cshtml' must derive from WebViewPage, or WebViewPage<TModel>.

Source Error: 


Line 7:  @{
Line 8:      //Html.RenderPartial("_EnquiryBreadCrumb", "Enquiry");
Line 9:      @Html.Action("_EnquiryBreadCrumb", "Enquiry")
Line 10: }
Line 11: 

Source File: c:\Projects\2012\AMT2014_Prototype\AMT2014_Prototype\Views\Search\ContactSearch.cshtml    Line: 9 

请参阅我的文件结构的屏幕截图:

enter image description here

我想修复此运行时错误并在页面上显示部分视图。我正在使用MVC 5.

1 个答案:

答案 0 :(得分:1)

有关如何解决问题的更详细说明,请参阅以下链接 - The view must derive from WebViewPage, or WebViewPage<TModel>

但基本上,问题在于,由于您将局部视图放在自定义位置,因此没有具有RAZOR包含的web.config。

尝试在partialviews文件夹中包含web.config的副本,看看它是否有效