-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
ASP.NET jQuery Cookbook (Second Edition) - Second Edition
By :
Model View Controller (MVC) is a design pattern that separates design (Model), presentation (View), and action (Controller). Because of its popularity with developers, Visual Studio provides ready templates that are used to create MVC projects.
Similar to web forms, jQuery can be included in MVC views using the <script> tag. In this example, however, let's take a look at the use of bundling for this purpose.
Bundling helps you reduce the number of HTTP requests made by the browser. It is a feature that allows style sheets, JavaScript, or other files to be combined together in a single file called a bundle. This combined file can be downloaded as one unit using a single HTTP request.



HomeController and click on the Add button:
Index action method, as shown in the following screenshot. Click on Add View... as shown in the following screenshot:

In the remaining recipes, when asked to create a MVC application, follow steps 1 to 6 as mentioned earlier.
File menu, launch NuGet by navigating to Project | Manage NuGet Packages. Select Microsoft.AspNet.Web.Optimization from the list of available packages. If the package is not visible, search for web.optimization, as shown in the following screenshot. Click on the Install button to download and install the latest version:
Scripts folder in the project and include the jQuery library files in the folder.Follow these steps to bundle jQuery in ASP.NET MVC:
BundleConfig class in the App_Start folder in the MVC project. If the file does not exist, create a new module (VB)/class (C#) in the App_Start folder, and name it BundleConfig.vb/BundleConfig.cs.BundleConfig.vb/BundleConfig.cs, add a namespace to System.Web.Optimization at the top of the file:For VB, the code is as follows:
Imports System.Web.Optimization
For C#, the code is as follows:
using System.Web.Optimization;
RegisterBundles method in BundleConfig as follows:For VB, the code is as follows:
Public Module BundleConfig
Public Sub RegisterBundles(ByVal bundles As BundleCollection)
bundles.Add(New ScriptBundle("~/Scripts/jquery").Include(
"~/Scripts/jquery-{version}.js"))
End Sub
End ModuleFor C#, the code is as follows:
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/Scripts/jquery").Include(
"~/Scripts/jquery-{version}.js"));
}
}RegisterBundles method:For VB, the code is as follows:
BundleTable.EnableOptimizations = True
For C#, the code is as follows:
BundleTable.EnableOptimizations = true;
Global.asax file, include the namespace for System.Web.Optimization, as shown in step 2 mentioned previously. Then, register the bundle in the Application_Start method as follows:For VB, the code is as follows:
BundleConfig.RegisterBundles(BundleTable.Bundles)
For C#, the code is as follows:
BundleConfig.RegisterBundles(BundleTable.Bundles);
System.Web.Optimization, as shown in the following code:For VB, the code is as follows:
@Imports System.Web.Optimization
For C#, the code is as follows:
@using System.Web.Optimization
<head> element as follows:@Scripts.Render("~/Scripts/jquery")Bundling is disabled in the debug mode by setting the debug attribute to true in the <compilation> element in the web.config file. To override this setting and enable bundling in the debug mode, set the EnableOptimizations property of the BundleTable class to true in the RegisterBundles method.
Unless EnableOptimizations is set to true, or the debug attribute is set to false, the files will not be bundled and the debug versions of the files will be used instead of the minified versions.
Bundling jQuery in ASP.NET MVC can be done by following these steps:
~/Scripts/jquery-{version}.js includes the development as well as the minified versions. The .vsdoc file, which is used by IntelliSense, is not included in the bundle.
The Using a CDN to load jQuery in MVC recipe
Change the font size
Change margin width
Change background colour