I am new to MVC.
I need to have two views (for same logic) one for normal browser and one for mobile.
How I can I redirect to different view for Mobile?
right now i have two controller but i want to use only one controller (HomeController.cs) for both views.
I know this way, I should have my mobile views under “Home”.
Please help me to redirect to Mobile view using only one controller.
i am using below for redirect for now (with two controllers):
return RedirectToAction(“Index”, “Mobile”);
I am using framework 3.5 and MVC 2
I’m having a little trouble picturing your setup. My guess is you have the current setup:
Controllers
– HomeController.cs
– MobileController.cs
Views
– Home
–Index.aspx
– Mobile
–Index.aspx
But you want:
Controllers
– HomeController.cs
Views
– Home
–Index.aspx
– Mobile
–Index.aspx
Is that correct?
Update:
Aas people mentioned below it’s one of those ‘that’s just how MVC works’ sort of deal. ‘Home’ is a location you can go to, but ‘Mobile’ is a specific type of page. If you add another area called “About” as new Views subfolder, where would you put your mobile folder? What is mobile now supposed to handle? If mobile is not supposed to replicate the regular site, then it should have its own controller, even if it replicates some code (don’t forget you can create classes outside the controllers that can do the brunt of the work that any controller can call).
On the other hand if you want a mobile version of each of your pages you should be adding them under the views folder for each route. For example:
Controllers
– HomeController.cs
Views
– Home
– Mobile
— Index.aspx
–Index.aspx
I’m not exactly sure where you are doing the logic to figure out if they are on a mobile platform, but assuming for now it’s in the controller somewhere in your HomeController.cs you then have: