1

How can I insert javascript in asp.net MVC application(view)?

I am doing following things in my javascript:

1) Read values from DOM elements.

2) Send data to Controler action method from Jquery Ajax.

3) Validating my input.

4) Read/Write cookies

How can I insert javascript in View.

Should it be something like this?

 <script src="../Scripts/Jquery-2.1.js" type="text/javascript"></script>
 <script src="../Scripts/jquery.cookie.js" type="text/javascript"></script>

<script>

       $(document).ready(function () 
          {

            $.cookie("pc", "Navada, US");

          }

        );

</script>

@{
    ViewBag.Title = "ViewPage1";
}
<h2>ViewPage1</h2>

Thanks for your answer

user786
  • 3,902
  • 4
  • 40
  • 72
  • 1
    Use the [``](http://weblogs.asp.net/scottgu/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax). – Andrei V May 22 '15 at 08:10
  • 1
    I don't understand this question. Can you somehow elaborate? – Liam May 22 '15 at 08:11
  • @AndreiV can you please elaborate what do you mean by – user786 May 22 '15 at 08:20
  • You can dynamically add text (e.g. JavaScript) to your view trough Razor, using the `` tag. The are a lot of examples out there. Start with the link in my first comment. – Andrei V May 22 '15 at 08:22
  • 1
    @Liam In webforms we use – user786 May 22 '15 at 08:22
  • @AndreiV Can you please explain with code as an answer? – user786 May 22 '15 at 08:23
  • @Alex create a simple MVC application that is in the templates of VIsual Studio and check out where is JS there. And about your question, yes you can write code in – slava May 22 '15 at 08:40
  • You can put script tags wherever you want, in fact in some scenarios it's better to put them at the bottom of the page to prevent blocking of the UI thread. So to answer your question, **yes your code is correct** (after a fashion) – Liam May 22 '15 at 08:41

1 Answers1

4

Not sure if i understand your question right, but if so you may need to put your script tags into the head tag.

To be able to put your custom js things into head you could try looking at this example:

In your _Layout page add this between <head></head>:

    @RenderSection("CustomScripts", required: false);

And on your page:

   @section CustomScripts 
   {
   <script src="@Url.Content("~/Scripts/foo.js")" type="text/javascript"></script>
   }

EDIT: as @Liam correctly pointed out that the _Layout file could be in any other path, @StephenMuecke's clarification applied.

kayess
  • 3,384
  • 9
  • 28
  • 45
  • 1
    A layout doesn't have to be in Views/Shared/_Layout, but basically do this. – Liam May 22 '15 at 08:42
  • 2
    It also does not have to between the `` tags and there are many arguments for why it should be placed immediately before the closing `` tag. And there is no point using `@if (IsSectionDefined("CustomScripts"))` because you have used `@RenderSection("CustomScripts", required: false);` which means it optional. The only reason for the `if` block is if you used just `@RenderSection("CustomScripts")` because the default value for `required` is `true` –  May 22 '15 at 08:48
  • @StephenMuecke thank you for the clarification. I will edit my answer. As for the debate about placing the script tags see [link](http://stackoverflow.com/questions/496646/how-does-the-location-of-a-script-tag-in-a-page-affect-a-javascript-function-tha) – kayess May 22 '15 at 08:54
  • _"As for the debate ..."_ Exactly my point - there are many arguments for and against. They can go in either the `` or immediately before the closing `` tag –  May 22 '15 at 09:02