7

Possible Duplicate:
ASP.Net MVC 3 Razor: Include js file in Head tag

I don't want to put a lots of JS into some layout and I need to do it for some specific pages I mean to include some of the JS into their header.

I've tried like that but it doesn't work as it should be.

@{
    Layout = "~/Views/Shared/_LayoutInner.cshtml";
    @Scripts.Render("~/Scripts/farbtastic/farbtastic.js")
    @Styles.Render("~/Scripts/farbtastic/farbtastic.css")
    @Scripts.Render("~/Scripts/jquery.tinycarousel.min.js")
    @Scripts.Render("~/Scripts/jquery-ui-1.8.11.min.js")
}
<script type="text/javascript" charset="utf-8">
    $(document).ready(function () {
        $('#slider1').tinycarousel();
        $("#accordion").accordion();
        $('#picker').farbtastic('#color');
    });
</script>

I have tried like that

@{
    Layout = "~/Views/Shared/_LayoutInner.cshtml";
  <script type="text/javascript" src="@Url.Content("~/Scripts/farbtastic/farbtastic.js")"></script>
<link href="@Url.Content("~/Scripts/farbtastic/farbtastic.css")" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.tinycarousel.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")"></script>
}

and no success at all.

How I can archive it?

7
  • 1
    Couldn't u define a head tag and refer JS in pages? Commented Aug 30, 2012 at 13:02
  • @ssilas777 Oh! How I can do it? Commented Aug 30, 2012 at 13:03
  • 3
    Check this question: stackoverflow.com/questions/4311783/… I think this is what you need. Commented Aug 30, 2012 at 13:04
  • 4
    As a side note Scripts.Render and Styles.Render are used in Bundling and Minification not to reference individual files. Commented Aug 30, 2012 at 13:07
  • 1
    Not really an answer, I got fed up with the System.Web.Optimization stuff between trying to put Modernizr in the header, and it sometimes not including new Javascript files, and switched to getcassette.net A slight learning curve, but it has quite a few benefits over the built in bundling stuff, IMHO. Commented Aug 30, 2012 at 13:13

1 Answer 1

13

I'm sure in your _LayoutInner.cshtml you should have refered JS files similarly like this

<head>
<link href="@Url.Content("~/Scripts/farbtastic/farbtastic.css")" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")"></script>
</head>

To achieve your target you have to add two named sections into your _LayoutInner.cshtml pages head section like this-

<head>
<link href="@Url.Content("~/Scripts/farbtastic/farbtastic.css")" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")"></script>
 @RenderSection("JavaScript", required: false)
 @RenderSection("CSS", required: false)
</head>

Now in your other pages to include extra javascript or css pages use these named sections

@section JavaScript
{
   <script type="text/javascript"src="@Url.Content("~/Scripts/farbtastic/farbtastic.js")"></script>
   <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.tinycarousel.min.js")"></script>
}

@section CSS
{
  <link href="@Url.Content("~/Scripts/farbtastic/farbtastic.css")" rel="stylesheet" type="text/css" />
}

It is upto you whether to include different named sections for javascript and css.

Hope it helps!

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much!!!! One last question: Have do I keep SECTIONS in some external files?
Nope you have to use sections in pages you want to include these JS files..In general the sections are not used only for including external files, it is meant to output dynamic content in these sections in the final response.In this case you can see javascript files and css files exactly in ur final response in the head section.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.