James
James

Reputation: 233

How to automatically override the view in MVC

We have an MVC site and I am now adding mobile views to it, optimized with jquery mobile. All the controllers and models will stay the same.

So lets say you have a typical controller that returns a view:

    public virtual ActionResult CustomerProfile()
    {                
       return View(CurrentCustomer);
    }

I want something to check to see whether a session variable is set that says IsMobileView == true, and if so, I want it to return the same view name... EXCEPT with the suffix "_Mobile" in the aspx filename.

So in the above case, lets say we decorate the method with the attibute [SupportsMobile], if the session variable is set it will use "CustomerProfile_Mobile.aspx" as the view. If it doesnt [SupportsMobile] or the session isn't set to be in mobile mode, it should just return CustomerProfile.aspx as normal.

This needs to work with the syntax return View("CustomerSearch", model) also, which would return CustomerSearch_Mobile.aspx if the conditions were right.

How do I go about putting this check in? I can obviously override various action events in the controller, but I need to intercept where the view is being loaded up.

Thanks in advance! James.

Upvotes: 1

Views: 1267

Answers (1)

Fals
Fals

Reputation: 6839

You don't need to use Session for this. You should use DisplayModeProvider or other modern api's like 51Degrees for this purpose. There's a lot of information from the ASP.NET Team

Mobile Apps & Sites with ASP.NET

Take a look and fallow the Architectural option that fits for your demand!

Upvotes: 2

Related Questions