views:

129

answers:

2

I am building what will primarily be a mobile browser targeted ASP.NET MVC application. Although it will target the desktop as well in a smaller capacity. (I'm fairly new to both MVC and mobile apps.)

I'm wondering what is the best practice for segregating mobile vs. desktop users in an MVC application.

Should the controller be in charge of checking for browser type? Or, should this type of functionality be reserved for the View?

If checked in the view, can & should a masterpage do the checking? Do you know of any good examples online?

Update: I just discovered an overload of the View method that accepts a string argument specifying the Masterpage to be used.

For example:

public ActionResult Index()
{    
  if (isMobile())
      return View("Index", "Mobile", myObject);
  else
      return View("Index", myObject);
}

To me this suggests that at least a few people on the Microsoft team expect major distinctions (such as mobile vs. desktop) to be carried out in the controller. (There's a good chance I'm highly confused about this.)

A: 

Any code dealing with rendering of your code should exist on the page itself via CSS and javascript. Your controllers should know nothing about how your data will be rendered on screen. The views shouldn't really even know anything about it either - they only expose the data that your CSS will render.

The HTML your View spits out describes your data and how it is organized. Only the CSS should know how to make it look appropriate for whatever device is rendering it.

This link, chock full of javascript should help determine which mobile browser is running.

Terry Donaghe
There will probably be expanded functionality when the browser is a desktop (additional options, etc). Doesn't seem like a job for javascript.
Traples
I think detect a mobile browser with js is not a good idea, because most mobile browser don´t support js. Try WURFL.So, ui is not only CSS and js, you should adapt image size too. I think a desktop and mobile user have differents needs.
fravelgue
Good points fravelgue - maybe to serve both desktop and mobile clients we need two different apps, each accessing the same middle-tier.
Terry Donaghe
+1  A: 

I think the controller must know the plataform, because you can get a lot of views in distint languages, Some view for browsers (mobile) another view in Desktop App, another view can be a web service, and all views can have different needs.

If you have a few views, you can call views with parameters to mark the type of view:

Index(mobile) and Index(Desktop) as it:

Index(string typeOfApp){
//prepare data, do querys, etc
if (typeOfApp=='Mobile'){
redirectoAction('IndexMobile',ds); 
//or 
return view('IndexMobile',ds)
}
return View('IndexDesktop',ds);
}

IndexMobile(DataSet ds){}

IndexDesktop(DataSet ds){}

You can get a general method for your action() and another action for every type, Index -> Index4Mobile & Index4Browser & Index4Desktop

And in all of this methods prepare or do something special for every plataform, or a Single Action with multiple views(1 for plataform).

Jorge Mota