Skip to content Skip to sidebar Skip to footer

How To Embed Javascript In Asp.net Mvc View

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 metho

Solution 1:

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.

Post a Comment for "How To Embed Javascript In Asp.net Mvc View"