c# - 部分 View '/Views/Shared/... 未找到或没有 View 引擎支持搜索的位置

标签 c# asp.net-mvc razor model-view-controller

<分区>

在 ASP.Net mvc cshtml 页面中,我有以下代码调用位于我的文件夹中的 4 个局部 View :

<div id="accordion">
     <h3 style="padding-left: 50px">Instructions</h3>
        <div>@Html.Partial("/Views/Shared/_instruct.cshtml", null)</div>

    <h3 style="padding-left: 50px">Projects by  @Html.DisplayFor(model => model.FirstName) @Html.DisplayFor(model => model.LastName)</h3>
        <div>@Html.Partial("~/Views/Shared/_research.cshtml", Model.Proposalsvm)</div>
   <h3 style="padding-left: 50px">  Publications</h3>
        <div>  @Html.Partial("~/Views/Shared/_publications.cshtml", Model.Pubs)</div>
    <h3 style="padding-left: 50px">  Patents</h3>
        <div>   @Html.Partial("~/Views/Shared/_patents.cshtml", Model.Patents)</div>
    <h3 style="padding-left: 50px">Training</h3>
        <div>@Html.Partial("~/Views/Shared/_Student.cshtml", Model.Students)</div>
    </div>

所有 View 都位于我的共享文件夹中,拼写正确。当我从我的本地机器运行代码时,所有的局部 View 都会被渲染。当我将代码部署到我的生产机器时,如果我注释掉第一个部分 View ,则会呈现最后三个部分 View 。如果我不这样做,我会收到以下错误:

The partial view '/Views/Shared/_instruct.cshtml' was not found or no view engine supports the searched locations. The following locations were searched:
/Views/Shared/_instruct.cshtml 
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/_instruct.cshtml' was not found or no view engine supports the searched locations. The following locations were searched:
/Views/Shared/_instruct.cshtml

Source Error: 
line 19:        <div id="accordion">
Line 20:          <h3 style="padding-left: 50px">Instructions</h3>
Line 21:             <div>@Html.Partial("/Views/Shared/_instruct.cshtml", null)</div>
Line 22:             
Line 23:         <h3 style="padding-left: 50px">Projects by  @Html.DisplayFor(model => model.FirstName) @Html.DisplayFor(model => model.LastName)</h3>

Source File: d:\WebSite\BUD\Views\PUBLICATIONS_forUD\BARDat40.cshtml    Line: 21 

Stack Trace: 
[InvalidOperationException: The partial view '/Views/Shared/_instruct.cshtml' was not found or no view engine supports the searched locations. The following locations were searched:
/Views/Shared/_instruct.cshtml]
   System.Web.Mvc.HtmlHelper.FindPartialView(ViewContext viewContext, String partialViewName, ViewEngineCollection viewEngineCollection) +416
   System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection) +225
   System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData) +136
   ASP._Page_Views_PUBLICATIONS_forUD_BARDat40_cshtml.Execute() in d:\WebSite\BUD\Views\PUBLICATIONS_forUD\BARDat40.cshtml:21

导致问题的部分 View 仅包含 div,没有模型。我尝试了以前答案中的解决方案,但没有一个奏效。 ( View 的构建是内容,我已经删除并重新创建了局部 View ,重命名了局部 View ,从@Html.Partial 中删除了 null,从 View 中删除了 cshtml - 这些都没有用。)

最佳答案

我觉得应该是

<div>@Html.Partial("~/Views/Shared/_instruct.cshtml", null)</div>

你错过了指示相对路线的 ~ tilda

关于c# - 部分 View '/Views/Shared/... 未找到或没有 View 引擎支持搜索的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51987362/

相关文章:

c# - Entity Framework Code First 多对多创建重复行

c# - 在 ASP.Net MVC Controller 中,如何获得一种方法来识别来自 JSON 和 Form-urlEncoded 格式的数据?

c# - 如何获取mvc3 Controller 中json数组中的值?

c# - 以对象列表作为属性的 MVC 模型

c# - 我需要一种优雅的方式来标准化标量(某种程度上)

c# - WPF 显示每个图像 x 秒如何?

asp.net-mvc - 说 ASP .NET MVC 应用程序是 HTTPModule 是否正确?

asp.net-mvc - 如何访问 _layout.cshtml 中模型的属性

Web API 中的 ASP.NET 标识

c# - 如何在 asp.net 中更新当前的 css 版本