【问题标题】:The partial view '/Views/Shared/ ... was not found or no view engine supports the searched locations [closed]部分视图'/Views/Shared/ ...未找到或没有视图引擎支持搜索的位置[关闭]
【发布时间】:2018-08-23 13:47:34
【问题描述】:

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

<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>

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

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

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

【问题讨论】:

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


    【解决方案1】:

    我觉得应该是的

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

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

    【讨论】:

    • 他不需要第二个参数,如果它是 null 我认为,真的吗?
      @Html.Partial("~/Views/Shared/_instruct.cshtml")
    • 不,他没有.. 但它不会受伤。由于默认值为null
    • 非常感谢。我盯着代码看了 4 个小时,没看到。
    • 有时花几个小时寻找更复杂的错误意味着您错过了非常简单的错误。如果可以的话,请考虑接受我的回答。快乐编程。 (有时休息一下,休息后问题就很容易发现了)
    猜你喜欢
    • 2014-03-30
    • 2015-04-24
    • 1970-01-01
    • 2013-06-23
    • 2013-08-18
    • 1970-01-01
    • 2017-03-09
    • 2015-07-09
    • 1970-01-01
    相关资源
    最近更新 更多