Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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

share|improve this question
1  
Use the <text>. – Andrei V May 22 '15 at 8:10
1  
I don't understand this question. Can you somehow elaborate? – Liam May 22 '15 at 8:11
    
@AndreiV can you please elaborate what do you mean by <text.> – Alex May 22 '15 at 8:20
    
You can dynamically add text (e.g. JavaScript) to your view trough Razor, using the <text> tag. The are a lot of examples out there. Start with the link in my first comment. – Andrei V May 22 '15 at 8:22
1  
@Liam In webforms we use <script> tag in header. In mvc view template there is no header template. But if there is, then correct me please. So my question is: the code in question is correct? I am using <script> tag at the top of view – Alex May 22 '15 at 8:22
up vote 1 down vote accepted

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.

share|improve this answer
1  
A layout doesn't have to be in Views/Shared/_Layout, but basically do this. – Liam May 22 '15 at 8:42
2  
It also does not have to between the <head> tags and there are many arguments for why it should be placed immediately before the closing </body> 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 – Stephen Muecke May 22 '15 at 8:48
    
@StephenMuecke thank you for the clarification. I will edit my answer. As for the debate about placing the script tags see link – kayess May 22 '15 at 8:54
    
"As for the debate ..." Exactly my point - there are many arguments for and against. They can go in either the <head> or immediately before the closing </body> tag – Stephen Muecke May 22 '15 at 9:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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