Display Mode Provider in MVC 5 Application

This article explains the display modes provider in ASP.NET MVC 5.

This article will solve a problem. Display modes in ASP.NET MVC 5 provide a way of separating page content from the way it is rendered on various devices, like web, mobile, iPhone, iPod and Windows Phones. All you need to do is to define a display mode for each device, or class of devices.

First you create a model and context class. We create an Employee class that has the following properties like.

  1. public class Employee  
  2. {  
  3.     public Guid ID { getset; }  
  4.     [Display(Name="First Name")]  
  5.     public string FirstName { getset; }  
  6.     [Display(Name = "Last Name")]  
  7.     public string LastName { getset; }  
  8.     [Display(Name = "Department")]  
  9.     public string Department { getset; }  
  10.     [Display(Name = "Salary")]  
  11.     public double Salary { getset; }  
  12.     [Display(Name = "Address")]  
  13.     public string Address { getset; }  

And second is the context class like this that inherits the DbContext class.
  1. public class DBConnectionContext:DbContext  
  2. {          
  3.    public DBConnectionContext(): base("name=dbContext")  
  4.    {  
  5.          Database.SetInitializer(new DropCreateDatabaseIfModelChanges  
  6.          <DBConnectionContext>());  
  7.    }  
  8.         public DbSet<Employee> Employees { getset; }  

If you want to recreate data every time the model changes, add these lines of code.

  1. Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DbConnectionContext>()); 

You also have a web config file. We configure connectionStrings in the Web.Config file.

  1. <connectionStrings>  
  2. <add name="dbContext" connectionString="Data Source=localhost; Initial Catalog=CommonDataBase; Integrated Security=true"   
  3.       providerName="System.Data.SqlClient" />  
  4.  </connectionStrings> 

Then you create a controller class in a controller folder and edit the name as HomeController. Add a Scaffold to select a MVC 5 Controller with Views, using Entity Framework.

DisplayModes

DisplayModes give you another level of flexibility on top of the default capabilities we saw in the last section. DisplayModes can also be used along with the previous feature so we will simply build off of the site we just created. Let's say we wanted to show an alternate view for the Windows Phone 8, iPhone, iPod or Android.

Windows Phone 8 DisplayMode

Now that you have made the override files, you can use a DisplayMode to show them for the appropriate phones.

The best time to set this up is when the application starts. Here is our global.asax.cs, with the DisplayMode setup.

  1. DisplayModeProvider.Instance.Modes.Insert(1, new DefaultDisplayMode("WP")  
  2. {  
  3.     ContextCondition = (ctx => ctx.GetOverriddenUserAgent()  
  4.     .IndexOf("Windows Phone OS", StringComparison.OrdinalIgnoreCase) > 0)    
  5.  });  
  6.    
  7. DisplayModeProvider.Instance.Modes.Insert(1, new DefaultDisplayMode("iPhone")  
  8. {  
  9.     ContextCondition = (ctx => ctx.GetOverriddenUserAgent()  
  10.     .IndexOf("iPhone", StringComparison.OrdinalIgnoreCase) > 0)    
  11. });  
  12.    
  13. DisplayModeProvider.Instance.Modes.Insert(1, new DefaultDisplayMode("Android")  
  14. {  
  15.     ContextCondition = (ctx => ctx.GetOverriddenUserAgent()  
  16.     .IndexOf("Android", StringComparison.OrdinalIgnoreCase) > 0)    
  17. });  

The DisplayModeProvider Class

DisplayModeProvider holds a list of DefaultDisplayMode obejects, each representing display mode. And the provider holds the two display modes, default and mobile. The default display mode in an empty string and the second holds the mobile string.

DisplayModeProvider

We just create multiple View with [View].Android.cshtml, [View].iPhone.cshtml and so on for every device such as:
 
multiple View
We create an index for iPhone and create a new employee in iPhone Index.iPhone.cshtml and Create.iPhone.cshtml.
 
index for iPhone

We create an index for Windows Phone and create a new employee in the Windows Phone Index.WP.cshtml Create.WP.cshtml.
 
Windows Phone

Window Phone Operating System

We create an index for Android and create a new employee in Windows Phone Index.Android.cshtml Create.Android.cshtml.
 
Windows Phone Index 
 
Create Android