2

Is there a way to prevent the script bundle from rendering on a specific view? I would like to use the _layout page and its script bundle by default on most of the views. I am having an issue with a specific view where I am using a jQuery grid and have included the scripts I need in that view. The bundle addition from the layout page is breaking my view with the grid.

2
  • can u access bundle from controller constructor ..i don't know much about bundling but if you are able to access than override bundleconfig with empty source. Nov 21, 2013 at 7:48
  • The bundle is in my _layout page and all my views include that _layout page by default so I'm not sure the override would work.
    – forwheeler
    Nov 21, 2013 at 13:44

2 Answers 2

2

You can access the name of the Action that returns the View through ViewContext.RouteData.

View:

~/Views/MyViews/MyAction.cshtml

Action:

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

And here's how you would access the Action name from inside your View:

string currentAction = ViewContext.RouteData["action"].ToString();

And check for it before rendering your Bundle:

@if(currentAction != "MyAction")
{
    @Scripts.Render(/* your script bundle here... */)
}

To further refine this, you can use similar methods to check for the name of your Controller if you need to do so.

Hope this helps.

1
  • Thanks for your help. Since I have the @Scripts.Render in my _layout page it will render the script even if I have this code in my view. If I remove the script rendering from my _layout page then I need to add the script back in all my other views. When I remove the _layout page from my view I do it like Layout = null; and I was hoping for something similar for the script rendering on this one view.
    – forwheeler
    Nov 21, 2013 at 13:36
0

Thanks kehrk. What I did was create another layout page without the Scripts.Render section, _LayoutPCA and modified the _ViewStart page similar to your code like this:

  string currentController = ViewContext.RouteData.Values["controller"].ToString();
if(currentController == "PCA")
{
    Layout = "~/Views/Shared/_LayoutPCA.cshtml";
}
else
{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

This way I can load the _LayoutPCA for this controller only and load the default layout page for the rest of the views. This will of course affect all the view for the PCAController but is ok for now.

1
  • Don't forget to select the answer that worked for you. Glad I could help.
    – keeehlan
    Nov 21, 2013 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.