简体   繁体   中英

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

I've this ActionResult:

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

ExpedientIIT View: https://jsfiddle.net/pq16Lr4q/

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

So when I return the view I got this error:

在此处输入图片说明

I tried to put console.logs to see if the view is rendered but is not rendered...

Ok the error is here:

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

Can you please help me.

You need to put the missing section inside your ExpedienteIIT view. According to the error message, that missing section is JavaScript .

Code sample, put this at the bottom of your view:

@section JavaScript
{
    // put javascript here
}

EDIT:

Thank you for providing a code sample of your views. There is a mismatch between how the JavaScript section in your layout page is defined and how it is being included in your view.

To fix this, do either one of the following:

  • In your _Layout page, change @RenderSection("scripts", required: false) to @RenderSection("JavaScript", required: false) , OR
  • In your ExpedienteIIT view, change @section JavaScript to @section scripts

The important thing is that the two should match.

edit: After reading your code, i feel like

@RenderSection("scripts", required: false)

should be

@RenderSection("JavaScript", required: false)

an other thing that I think will give you trouble is the fact that you define your "JavaScript" section in the body. This means that if any of your views you forget to add that

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

you'll get a Section JavaScript not defined error. In your case, feels like the section's definition should be in the _layout.cshtml .

This error most likely means that you have defined the JavaScript section but have not rendered it anywhere. You need to call @RenderSection("JavaScript") somewhere in your layout.cshtml

the

@section JavaScript
{
}

will let you create a section called "JavaScript", but to actually "print" the content of this section to the output HTML file (that will be sent to the client) you need to call @RenderSection("JavaScript") . The content of the section will be printed where the call to RenderSection is located.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM