Making a switchable Desktop and Mobile site with ASP.NET MVC 4 and jQuery Mobile?
Making a switchable Desktop and Mobile site with ASP.NET MVC 4 and jQuery Mobile?
I really enjoy working on and thinking about mobile websites. There's something about making an experience great on a pocket supercomputer phone that is so much more satisfying than a desktop. I actually got this blog looking nice on mobile devices back in 2006 when nobody was mobile except doctors and, well, all of us techies here on the blogs.
I've talked about the importance of a good mobile site before in posts like Create a great mobile experience for your website today. Please. However, some folks had asked me if I'd do a post on how to do a combination Desktop and Mobile site using ASP.NET MVC similar to the examples I used in my talks in Russia on mobile earlier this year. (There's video of those ASP.NET mobile presentations available)
When you start Visual Studio 2012 and go File | New ASP.NET MVC 4 app, there's an Internet Application template and a Mobile template. One gets you a standard desktop site - although with responsive design elements so it works on small screens - and the other gets you a jQuery Mobile application meant primarily for phones and tablets. Let's make one that switches between both.
We will do a small site in ASP.NET MVC for the Desktop, do some quick DB access, add jQuery Mobile and a View Switcher switch back and forth. I'll be using the Electric Mobile Studio from Electric Plum to simulate an iPhone. You can get a 7 day trial or you can get the Lite version of the Electric Plum Mobile Simulator with WebMatrix 2.
QUICK CRUD EXAMPLE
First, a model for DVDs.
public class DVD
{
public int ID { get; set; }
public string Title { get; set; }
public int Year { get; set; }
public Rating rating { get; set; }
}
public enum Rating
{
G, PG, PG13, R
}
Next, I scaffold out the Index, Create, Edit, Delete, etc. Unfortunately scaffolding doesn't do Enums (I'm sad) for my Movie Ratings so I add EditorFor() calls to my Create and Edits, and update my Index.
<div class="editor-label">
@Html.LabelFor(model => model.rating)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.rating)
@Html.ValidationMessageFor(model => model.rating)
</div>
Shared/EditorTemplates/Rating.cshtmlI could have used DropDownList I suppose, but I have always found that helper confusing. Instead, I'll create a Rating.cshtml that makes a dropdown. I could change this at some future point to be fancier and not even use a DropDown.
Aside: How do you guys usually handle Enums? I've seen it done a few ways. I'd like this to be more generic, but here's what I did for the Rating editor Template. Note the nullable? As it has to work for both Create and Delete
@model MyApp.Models.Rating?
@using MyApp.Models
<select id="@ViewData.TemplateInfo.GetFullHtmlFi… Name="@ViewData.TemplateInfo.GetFullHtml…
@foreach (int rating in Enum.GetValues(typeof(Rating. {
var name = Enum.GetName(typeof(Rating), rating);
<option value="@name" selected="@(Model.HasValue? (int)Model == rating: false)">@name</option>
}
</select>
OK, so there's a basic Desktop CRUD app.
Editing a DVD List of DVDs
MAKING IT MOBILE
iPhone in the Visual Studio browser menuAs I mentioned, you've probably noticed when making an ASP.NET MVC application that you can choose a Mobile template with jQuery Mobile along with the standard responsive "desktop" Internet Application. However, there isn't a switchable template. That is, one that is the regular template on Desktops but switches to jQuery Mobile (or KendoUI, or whatever makes you happy) on mobile devices.
Using NuGet, install the jQuery.Mobile.MVC package. You can either right click on References, select Manage NuGet Packages, or you can use the Package Manager Console and type:
install-package jQuery.Mobile.MVC
This package will automatically bring in jQuery Mobile as well as:
A ViewSwitcher partial view and supporting Controller
This is what lets us switch manually between Desktop and Mobile
A basic _Layout.Mobile.cshtml and supporting stylesheet
A BundleMobileConfig.cs used with ASP.NET Optimization (the bundler for CSS and JS)
NOTE: There's nothing jQuery Mobile specific about the ViewSwitcher or the techniques here. You can happily change this package for any other popular mobile framework.
We start by adding the new Bundles in the Global.asax:
BundleConfig.RegisterBundles(BundleTabl…
BundleMobileConfig.RegisterBundles(Bund… // <-- ADD THIS ONE
Since I installed the Electric Plum iPhone simulator, I'll select it from my browser dropdown in Visual Studio and run my app and navigate to /DVD.
The system sees that I'm on a mobile device and rather than using _Layout.cshtml it uses _Layout.mobile.cshtml.
This doesn't really look nice for a few reasons. First, I don't like the default style. Just go into _Layout.Mobile.cshtml and change the data-theme attribute to a value that makes you happy. I changed it from theme a to theme b.
- Website urls redirect to mobile site homepage? I have a little problem, if a person using a mobile device clicks on a link from the full website it redirects them to the mobile site homepage. Is there a way to correct this? I have the same permalink set for the page on both the full and mobile site but if I share a link on facebook and I click on it using an iphone I go right to the mobile homepage. How do you redirect to the correct page instead of homepage?
- View desktop site from IPhone? I'm trying to view a website from my iPhone 5, but I need to view the full site/ desktop version. And there's defiantly to "view full site button" anywhere! Is there and way to view the full site? Like maybe a 3rd party website I can go through or an app?
- Safari on iphone keeps pushing desktop instead of mobile sites? My iPhone (safari) keeps redirecting me to the desktop version of youtube instead kf the mobile version. It's really annoying, please help. Also, i would just like to add, i know that there's a "mobile version" link in the bottom corner, but when i go to my bookmark, it goes straight back to the desktop version.
- Mobile web site development - images and device orientation? I have developed a mobile site with a 320px wide image on the page. When viewed on the iPhone in portrait orientation, all is. But when you view in landscape mode, it obviously doesn't fit the page. How to fix so that the image will fit all devices, regardless of orientation?