以下部分已定义但尚未为布局页面“~/Views/Shared/_Layout.cshtml”呈现

The following sections have been defined but have not been rendered for the layout page “~/Views/Shared/_Layout.cshtml”

我有这个 ActionResult:

[EncryptedActionParameter]
[CheckExternalUserRegisterSigned]
public ActionResult ExpedienteIIT(int idExpediente)
{
    ExpedienteContainerVM model = this.GetExpedienteVMByIdExpediente(idExpediente);
    return View("ExpedienteIIT", model);
}

ExpedientIIT 视图: https://jsfiddle.net/pq16Lr4q/

_Layout.cshtml: https://jsfiddle.net/1ksvav43/

所以当我 return 视图时我得到了这个错误:

我试过放console.logs看view是渲染了还是没有渲染...

好的,错误在这里:

@model PortalSOCI.WEB.ViewModels.IIT.ExpedienteContainerVM


    @{
        ViewBag.Title = String.Format(PortalSOCI.WEB.Resources.ExpedienteIIT.TituloExpedienteIIT, Model.Expediente.NumeroExpediente);
    }

    @section JavaScript  /// <-------------------- ERROR
    {
        @Html.Raw(ViewBag.message)
    @

你能帮帮我吗

您需要将缺失的部分放入 ExpedienteIIT 视图中。根据错误消息,缺少的部分是 JavaScript.

代码示例,将其放在视图的底部:

@section JavaScript
{
    // put javascript here
}

编辑:

感谢您提供您的观点的代码示例。布局页面中 JavaScript 部分的定义方式与其在视图中的包含方式不匹配。

要解决此问题,请执行以下任一操作:

  • 在您的 _Layout 页面中,将 @RenderSection("scripts", required: false) 更改为 @RenderSection("JavaScript", required: false),或者
  • 在您的 ExpedienteIIT 视图中,将 @section JavaScript 更改为 @section scripts

重要的是两者要匹配。

编辑: 看完你的代码,我觉得

@RenderSection("scripts", required: false)

应该是

@RenderSection("JavaScript", required: false)

我认为会给您带来麻烦的另一件事是您在正文中定义 "JavaScript" 部分。这意味着如果您忘记添加任何观点

@section JavaScript
{
    @Html.Raw(ViewBag.message)
}

您将收到 Section JavaScript not defined 错误。在您的情况下,感觉该部分的定义应该在 _layout.cshtml.

这个错误很可能意味着您已经定义了 JavaScript 部分,但没有在任何地方呈现它。 您需要在 layout.cshtml

中的某处调用 @RenderSection("JavaScript")

@section JavaScript
{
}

将让您创建一个名为 "JavaScript" 的部分,但实际上要将此部分的内容 "print" 输出到输出 HTML 文件(将发送给客户端),您需要打电话给 @RenderSection("JavaScript")。该部分的内容将打印在对 RenderSection 的调用所在的位置。