如何正确获取嵌套文件夹以在MVC 5 C中呈现#

时间:2017-04-08 23:47:20

标签: c# asp.net-mvc asp.net-mvc-5 .net-4.5

我已经获得了传统网站,所有MVC 5都按预期工作,所有内容都正确呈现并且工作正常。

我已经添加了一个仪表板和这个仪表板,我试图设置文件夹结构,直到它与MVC 5 C#网站的其余部分隔离;但是,我收到服务器错误,指出布局引擎无法找到我的部分视图。

现在,当视图本身加载时,它正在通过测试并确实返回正确的视图,当布局引擎无法找到嵌套的部分视图时,问题就出现了。

由于我没有完全了解MVC 5,我很遗憾。

现在,就像之前提到的那样,在开箱即用文件夹结构方面一切都运行良好但是当我创建一个嵌套文件夹并放置部分视图时,MVC 5引擎无法找到这些嵌套的部分视图。

嵌套这些部分视图的原因很明显我将实际的传统网站与用户仪表板视图分开。

当我单击指向索引文件的操作链接时,这可以正常工作,但当正文,查看,试图渲染它时无法找到嵌套的部分视图。

我在这里缺少什么?我希望我提供足够的信息,以便尽可能简单地回答这个问题。

加载实际用户信息中心索引

 @{
    ViewBag.Title = "Your Dashboard";
    Layout = "~/Views/Shared/UserDashboard/_Layout_Dashboard.cshtml";
}
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">

加载实际的部分是服务器拒绝的地方,您没有正确执行此操作。

This Snapshot Is The _Layout

以下是精确服务器错误,请告知我正确实施此方法。

Server Error in '/' Application.
The partial view '~/Views/Shared/UserDashboard/_PartialHeader_Dashboard' was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Shared/UserDashboard/_PartialHeader_Dashboard
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 partial view '~/Views/Shared/UserDashboard/_PartialHeader_Dashboard' was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Shared/UserDashboard/_PartialHeader_Dashboard

Source Error:


Line 26: <script src="http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>*@
Line 27: <body>
Line 28:     @Html.Partial("~/Views/Shared/UserDashboard/_PartialHeader_Dashboard")
Line 29:     @Html.Partial("~/Views/Shared/UserDashboard/_PartialLeftNav_Dashboard")
Line 30:     @RenderBody()


Source File: D:\Users\Erik\Documents\visual studio 2017\Projects\BuilderBits\BuilderBits\Views\Shared\UserDashboard\_Layout_Dashboard.cshtml    Line: 28

Stack Trace:


[InvalidOperationException: The partial view '~/Views/Shared/UserDashboard/_PartialHeader_Dashboard' was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Shared/UserDashboard/_PartialHeader_Dashboard]
   System.Web.Mvc.HtmlHelper.FindPartialView(ViewContext viewContext, String partialViewName, ViewEngineCollection viewEngineCollection) +327
   System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection) +264
   System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData) +91
   System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName) +32
   ASP._Page_Views_Shared_UserDashboard__Layout_Dashboard_cshtml.Execute() in D:\Users\Erik\Documents\visual studio 2017\Projects\BuilderBits\BuilderBits\Views\Shared\UserDashboard\_Layout_Dashboard.cshtml:28
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +105
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +90
   System.Web.WebPages.<>c__DisplayClass3.<RenderPageCore>b__2(TextWriter writer) +232
   System.Web.WebPages.HelperResult.WriteTo(TextWriter writer) +10
   System.Web.WebPages.WebPageBase.Write(HelperResult result) +80
   System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body) +63
   System.Web.WebPages.WebPageBase.PopContext() +237
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +98
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +52
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +173
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9765045
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

1 个答案:

答案 0 :(得分:3)

当您尝试使用@Html.Partial("_PartialHeader_Dashboard")包含部​​分视图时,MVC框架将尝试根据MVC约定从以下位置加载部分视图。

  1. ~/Views/DirectoryWithYourCurrentControllerName/
  2. ~/Views/Shared/
  3. 框架不知道您的部分视图位于子目录中,除非您通过扩展视图引擎并指定视图文件位置(ViewLocationFormats)来告诉框架。您只需添加自定义位置即可。

    您有2个选项

    使用目录名称(位于shared

    下调用部分视图
    @Html.Partial("UserDashboard/_PartialHeader_Dashboard")
    

    使用完整文件路径

    @Html.Partial("~/Views/Shared/UserDashboard/_PartialHeader_Dashboard.cshtml")
    

    但更好的选择是考虑Areas。存在用于逻辑保存事物(控制器/视图等)的区域。