The following sections have been defined but have

2019-09-16 02:07发布

问题:

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.

回答1:

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.



回答2:

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.