How to use jquery in asp.net mvc 4?

I am working on a site using ASP.Net 4 with MVC. I am very familiar with WebForms and javascript, and used jquery, but linking me is confusing. From what I can tell, all javascript files are wrapped in BundleConfig.cs and loaded with a header in the site.master file. So, I should just add my additional functions and jquery calls to the new tags.

But it’s obvious that the way jquery functions should go into files is completely different from what I'm used to, and I can’t get anything to work at all.

Are there any short tutorials that can show me the correct way to do this in MVC? And not Razor, I don’t need two learning curves here.

+7
source share
2 answers

There is no β€œright” way to do this in MVC; it is up to you.

There are several common ways to do this.

You can place jQuery built-in functions inside your views by placing them in script tags for example.

 <script> $('a').click(function() { alert("Click"); } ); </script> 

However, this is usually not recommended, as it simplifies JS management and reuse and inflates your HTML. You probably would have done this only if you had a real little piece of script and it didn't feel like creating a new file.

Alternatively, you can put your html in separate js files and include a link to them in your views, for example.

<script src="~/Scripts/mycusomtjquery.js"></script>

This is better since it allows you to reuse and not inflate your HTML, and the browser can cache it.

An even better option is to use the Bundling function in MVC 4 and use it. Put js in a separate file, register it as a kit and do it in your view

eg.

Create js in separate files and put your own code there.

Put it in App_Start \ BundleConfig.cs

 bundles.Add(new ScriptBundle("~/bundles/mycustomjquery") .Include("~/Scripts/mycustomjquery.js") .Include("~/Scripts/myothercustomjquery.js")); 

Add a call to render the package in your view

 @Scripts.Render("~/bundles/mycustomjquery") 

This will make your js a miniature and unified way that can be cached by the browser.

You can customize this. Choose how to assemble your js files, how many actual js files to display, etc.

+11
source

I do not understand your problem, but do you know what a package is? First, take a look at this artciel to understand the whole function: http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification

You do not need to use the bundles in asp.net mvc. You can simply remove it from the project and easily add your javascripts files to your layout page. You can manually add the layout to the page and add your functions to another js file and include it in the View (or Layout) too. Asp.Net MVC is another suitable compared to WebForms.

Razor is Viewengine, and you can write server-side code using @ char for the sample: @Html.TextBox("Name") , which we have with asp.net mvc 3, but you can also write an ASPX mechanism for Sample: <%: Html.TextBox("Name") %> , since you are doing something in WebForms. (I recommend Razor because it’s faster to write, read and use).

Read more about ViewEngines here: http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx

+1
source

All Articles