Is there a correct way to add custom Javascript to an ASP.NET MVC 5 page?

yesman

At the moment, I have added the jQuery source file to my ASP.NET project's Scripts folder. In the _Layout.cshtml page, I have included ~/Scripts/jquery-2.1.1.min.js. Right now, I can include jQuery code on every page I make this way:

If this page shows a popup I was succesfull.

<script>
$(document).ready(function(){
alert("works!");
});
</script>

However, I don't want to include a full script in every view. I would prefer to create a seperate JS file, put it in my Scripts folder, and then include it using Razor.

@{ 
    //Razor Magic inserting Javascript method!
 }

If this page shows a popup I was succesfull.

How do I do this? And is it the "correct" way to include a unique Javascript file for a single page?

Kartikeya Khosla

Best and elegent way to solve your problem in MVC is making Bundles Known as Bundling and Minification.

See more here :- http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Correct, idiomatic way to use custom editor templates with IEnumerable models in ASP.NET MVC

What is the correct way to use nhibernate transactions and second level cache on an asp.net-mvc page?

Dependency Injection in asp.net 5 custom classes, what is the correct way?

Asp net mvc 5 add security to a jsonresult?

how to add model to razor page with jquery ajax in asp.net mvc 5?

Correct Way to use an async Library with ASP.NET/MVC/REST

Add JavaScript files in asp.net mvc

Add Javascript to header section of an ASP.NET/MVC web page from a view with bundling?

What the preferred way to add view-specific JavaScript to an Asp.Net Core 2 MVC application?

ASP.NET 5 MVC6 Custom CSS & Javascript placing convention

Custom 401 error page for ASP.NET MVC application

Custom 403 error page in ASP.NET MVC 2

ASP.NET MVC custom error page not working correctly

ASP.NET MVC 4 Custom 404 Page

Asp.net MVC Access Custom Error Page Without Authorization

ASP.NET MVC Custom user fields on every page

asp.net core mvc custom registration page

Add custom word in the URL of Route attribute ASP.NET MVC

How to add custom css files to asp.net mvc?

How to add custom HTTP Headers in Asp.Net-MVC

How to Add Custom Fields to a Json Result - ASP.NET MVC

How to add page list in asp.net core MVC application?

add a if clause to view page in asp.net mvc

Creating a custom route hierarchy ASP.NET MVC 5

Asp.net 5 MVC 6 Custom authentication

Asp.Net MVC 5 custom action routing under an area

Asp.Net MVC 5 - Custom Authorize not working?

Imlementing a Custom IRouter in ASP.NET 5 (vNext) MVC 6

How to implement custom authentication in ASP.NET MVC 5