123

I want to include a javascript reference like:

<script src="@Url.Content("~/Scripts/jqueryFoo.js")" type="text/javascript"></script>

If I have a Razor View, what is the proper way to include this without having to add it to the Layout (I only need it in a single specific View, not all of them)

In aspx, we could use content place holders.. I found older examples using aspx in mvc but not Razor view..

tereško
  • 58,060
  • 25
  • 98
  • 150
dferraro
  • 6,357
  • 11
  • 46
  • 69

4 Answers4

204

Depending how you want to implement it (if there was a specific location you wanted the scripts) you could implement a @section within your _Layout which would enable you to add additional scripts from the view itself, while still retaining structure. e.g.

_Layout

<!DOCTYPE html>
<html>
  <head>
    <title>...</title>
    <script src="@Url.Content("~/Scripts/jquery.min.js")"></script>
    @RenderSection("Scripts",false/*required*/)
  </head>
  <body>
    @RenderBody()
  </body>
</html>

View

@model MyNamespace.ViewModels.WhateverViewModel
@section Scripts
{
  <script src="@Url.Content("~/Scripts/jqueryFoo.js")"></script>
}

Otherwise, what you have is fine. If you don't mind it being "inline" with the view that was output, you can place the <script> declaration within the view.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • 3
    BTW, sections are essentially your contentplaceholders you were referring to. See the default MVC web project and how they place a heading on the page. – Brad Christie Jan 11 '13 at 18:57
  • thanks. This was what I was looking for - but is it possible to do this without the RenderSection? You say 'what you have is fine' - but I don't have anything yet... I tried putting the script references at the top of the .cshtml, but the result is the references are in the but they should be in the head – dferraro Jan 11 '13 at 19:04
  • 3
    @dferraro: then you need to add `RenderSection("Scripts")` to your layout (like you would a placeholder) and then define a `@section Scripts {}` within the view. At some point a modification to the "master" (_layout) is imminent. You can't just define something within a view and tell it "place it between `` for me" (unless you want to get in to a script that adds a script) – Brad Christie Jan 11 '13 at 19:06
  • 2
    +1. Also @dferraro a better thing would be to put the references to jQuery and the RenderSection before the

    and not in the head at all. Old but relevant reading: http://developer.yahoo.com/blogs/ydn/posts/2007/07/high_performanc_5/

    – MikeSmithDev Jan 11 '13 at 19:15
  • 1
    If your script is not in the `Scripts` folder then you might need to also enable access to it: http://stackoverflow.com/questions/24763493/how-to-include-js-files-in-the-view-asp-net-mvc-4 – Homer Aug 23 '16 at 20:37
  • Solution to **add custom script optionally** with use of if script is defined, check [this](http://stackoverflow.com/a/7357102/2218697) – Shaiju T Sep 10 '16 at 12:09
  • Now I understand the concept of Sections. Very powerful. – jboi Jan 01 '19 at 11:47
13

If you are using Razor view engine then edit the _Layout.cshtml file. Move the @Scripts.Render("~/bundles/jquery") present in footer to the header section and write the javascript / jquery code as you want:

@Scripts.Render("~/bundles/jquery")
<script type="text/javascript">
    $(document).ready(function () {
        var divLength = $('div').length;
        alert(divLength);
    });
</script>
Dilip Nannaware
  • 1,410
  • 1
  • 16
  • 24
9

You can add the script tags like how we use in the asp.net while doing client side validations like below.

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<script type="text/javascript" src="~/Scripts/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
    $(function () {
       //Your code
    });
</script>
0

You should add datatable.js script on defer="defer"

<script src="https://cdn.datatables.net/1.10.8/js/jquery.dataTables.min.js" defer="defer"></script>
vahid tajari
  • 1,163
  • 10
  • 20