1
votes

I have an MVC 4 site with an area. I setup two script bundles, one for all common scripts the site will need and the other for scripts related to area. I have a common layout view for the site where the common site bundles are referenced using Scripts.Render(). I have a second layout view for the area, which uses the common layout view, where I would like to render the area-specific javascript files, but it does not work and no files are rendered at all. If I move the bundle render to the root layout view, it renders fine.

Any reason why this does not work in the area view and how I can get it to work? I'd rather not have these area-specific scripts available for all users, since only a very small, defined subset actually need them.

Common Layout View:

@Scripts.Render(@"~/Scripts/all_scripts")
...
@RenderSection("Javascript", required: false)

Area Specific View:

@section Javascript {
    @Scripts.Render(@"~/Scripts/area_scripts")
    @RenderSection("Javascript", required: false)
}
1

1 Answers

2
votes

I tested this and it appears to be working fine. Admin specific scripts are rendered in the admin index view (and works in the admin layout too). Are all the bundles/paths configured correctly?

BundleConfig.cs

        bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                    "~/Scripts/jquery-ui-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryadmin").Include(
                    "~/Scripts/Admin/jquery.unobtrusive-ajax-admin*",
                    "~/Scripts/Admin/jquery.validate-admin*"));

_Layout.cshtml

    @Scripts.Render("~/bundles/jqueryui")
    @RenderSection("Javascript", required: false)

_AdminLayout.cshtml

    @{
        ViewBag.Title = "_AdminLayout";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

    <h2>AdminLayout</h2>
    @section Javascript{
        @RenderSection("Javascript", required: false)
    }

AdminHome/Index.cshtml

    @{
        ViewBag.Title = "Index";
        Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
    }

    <h2>Index</h2>


    @section Javascript{
        @Scripts.Render("~/bundles/jqueryadmin")
    }

Hope this helps.