2

I have a problem with the following. I have a masterpage which contains the main layout of the site. Then I have a specific view which uses the masterpage as a layout but adds additional scripts like the following:

_Masterpage.cshtml

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title - My ASP.NET MVC Application</title>
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <meta name="viewport" content="width=device-width" />
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/modernizr")
        @RenderSection("head", required: false)
    </head>
    <body>
        <header> ....

_Register.cshtml

@{
    Layout = "_Master.cshtml";
}

@section head
{
    @Scripts.Render("~/bundles/jqueryval");
    @Styles.Render("~/Content/themes/base/css");
}

<div id="body">
    <div data-role="page">
        @RenderBody()
    </div>
</div>

Now my view which contains the form data makes use of the _Register.cshtml, however when the Index in the RegisterController returns the view (like below), the scripts and styles in the _Register.cshtml are not found anywhere in the Head tag.

public ActionResult Index()
{
    return View();
}

Can anyone help me please? What am I missing in order to get these scripts/styles in the head tag please?

Thanks

tereško
  • 58,060
  • 25
  • 98
  • 150
jesmond
  • 125
  • 1
  • 6
  • is your _Register.cshtml a partial view? If not, then put all your scripts in master page. – Yusubov Dec 17 '12 at 18:54
  • why? I do not want to load all the scripts when I dont need them. Only want them added to the site when I am on a particular page. Is this possible? – jesmond Dec 17 '12 at 18:56

1 Answers1

1

Sections don't work in partial views and that's by design. You may use some custom helpers - Using sections in Editor/Display templates - to achieve similar behavior, but honestly it's the view's responsibility to include the necessary scripts, not the partial's responsibility. I would recommend you using the @scripts section of the main view to do that and not have the partials worry about scripts

Community
  • 1
  • 1
viperguynaz
  • 12,044
  • 4
  • 30
  • 41