Introducing Mobile Site in MVC 5 and jQuery Mobile


As you know there are various types of emulators available for viewing applications. We can use the iPhone and Windows Phone simulators for browsing the application. You can open the website on a phone that is so much more satisfying than a Desktop.

In that context, when you create the MVC 4 application you can use a Mobile template to design the application. Here, I am using Visual Studio 2013 and a MVC 5 project template to design the application. We'll use the jQuery Mobile application for displaying it in the phones and tablets.

Let's create an application on Visual Studio using MVC 5 Project template and perform some CRUD operations and add jQuery Mobile and ViewSwitcher. I am using the iPhone simulator to browse the application.

Creating CRUD Operations

Step 1: Add a model class named Cricketer and add the following code:

  1. public enum Grade  
  2. {  
  3.     A,B,C  
  4. }  
  5. public class Cricketer  
  6. {  
  7.     public int ID { getset; }  
  8.     public string Name { getset; }  
  9.     public string Team { getset; }  
  10.     public Grade Grade { getset; }  
  11. }  
  12. public class CricketerDbContext : DbContext  
  13. {  
  14.     public DbSet<Cricketer> Cricketers { getset; }  
  15. }

In the code above, we've created an Enum property and we'll add the Enum support to the View in this article later.

Step 2: Scaffold a New Controller

Scaffolding in MVC 5

Step 3: Unfortunately scaffolding does not do the Enums in the Create.cshtml and Edit.cshtml pages. You can see in the following screenshot:

Creating Records in Mvc

Step 4: So we need to update it using the following highlighted code:

  1. <div class="form-group">  
  2.     @Html.LabelFor(model => model.Grade, new { @class = "control-label col-md-2" })  
  3.     <div class="col-md-10">  
  4.         @Html.EnumDropDownListFor(model => model.Grade)  
  5.         @Html.ValidationMessageFor(model => model.Grade)  
  6.     </div>  
  7. </div>  

Step 4: Ok. Now it's time to run the application and perform the CRUD operations.

Enum Support in Mvc 5

Records in Mvc 5 App

Adding Mobile Support

You can develop it while creating the MVC 4 application with the Mobile Template but you can add the jQuery Mobile along with the MVC 5 application. You can simply have it from the NuGet Pacakges or entering the following command in the Pacakge Manager Console:

Install-Package jQuery.Mopbile.MVC

This package will add various things such as:

  • A ViewSwitcher partial view and supporting Controller
  • Basic _Layout.Mobile.cshtml and supporting CSS
  • Newly added BundleMobileConfig.cs

Note: You can use jQuery in any mobile framework.

Now you need to modify the Global.asax file with the following highlighted code:

  1. protected void Application_Start()  
  2. {  
  3.     AreaRegistration.RegisterAllAreas();  
  4.     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);  
  5.     RouteConfig.RegisterRoutes(RouteTable.Routes);  
  6.     BundleConfig.RegisterBundles(BundleTable.Bundles);  
  7.     BundleMobileConfig.RegisterBundles(BundleTable.Bundles);  
  8. }

When you run the application on the iPhone simulator as in the following:

Suimulators in Visual Studio

The following screenshot will open:

Default Page in iPhone Simulator

Now change the table in the Index.cshml page with the following code:

  1. <ul data-role="listview" data-filter="true" class="my_class_list">  
  2. @foreach (var item in Model)  
  3. {  
  4.     <li>  
  5.         <a href="@Url.Action("Details", new {item.ID})">  
  6.             <h3>@Html.DisplayFor(modelItem => item.Name)</h3>  
  7.             <p>@Html.DisplayFor(modelItem => item.Team) - @Html.DisplayFor(modelItem => item.Grade) Grade</p>  
  8.         </a>  
  9.     </li>  
  10. }  
  11. </ul>

Now just refresh the page.

jQuery mobile View


This article provided an overview of the jQuery Mobile application in the ASP.NET MVC 5 project template using Visual Studio 2013. Thanks for reading.

Similar Articles