开发者

ASP.NET MVC3 - How to serve View() from another controller

So in order accomplish what I asked in this post I did the following:

    [iPhone]
    [ActionName("Index")]
    public ActionResult IndexIPhone()
    {
        return开发者_运维百科 new Test.Areas.Mobile.Controllers.HomeController().Index();
    }

    [ActionName("Index")]
    public ActionResult Index()
    {
        return View(); 
    }

Which still serves the same view as the Index action method in this controller. Even though I can see it executing the Test.Areas.Mobile.Controllers.HomeController().Index() action method just fine. What's going on here? And how do I serve the Index view from Mobile area without changing the request URL (as asked in the original post referenced above)?


You have a few options:

  1. Redirect to the Action you'd like to return: return RedirectToAction("Action-I-Want").
  2. Return the View by name: return View("The-View-I-Want").

Note that with the 2nd approach you'd have to put your view in the "Shared" folder for all controllers to be able to find it and return it. This can get messy if you end up putting all your views there.

As a side note: The reason your work doesn't find the view is because default view engine looks for the view in the folder that "belongs" to the current executing controller context, regardless of what code you're calling.

Edit:

It is possible to group all "mobile" views in the same folder. On your Global.asax (or where ever you're setting up your ViewEngine, just add the path to your mobile View in the AreaViewLocationFormats. Mind you, you'll still have to name your views differently.

You can also write your own view engine. I'd do something like detecting the browser and then serving the right file. You could setup a convention like View.aspx, and View.m.aspx.

Anyhow, just take a look at WebFormViewEngine and you'll figure out what works best for you.


The easiest way to send a request to a view handled by another controller is RedirectToAction("View-Name", "Controller-Name").

There are overloads of View() that take route information that might work as well, but they'd require more effort to set up.


Well actually the easiest way is to make one version of your site programmed on standards instead of browser detection :D -- however in direct response to accomplish what it in a more of a ASP.NET mvc fashion, using:

RedirectToAction("ViewName", "ControllerName"); 

is a good method however I have found it is more practical if you feel you must program for different browser standards to create a primary view and an alternate "mobile" view under your controllers views. Then instead of writing special code on every controller, instead extend the controller like so.

public class ControllerExtended : Controller 
{
    private bool IsMobile = false;
    private void DetectMobileDevices(){ .... }
}

Then modify your controller classes to instead say ControllerExtended classes and just add the one line to the top of each Action that you have alternate views of like so:

public class ApplicationsController : ControllerExtended
{
    // GET: /Applications/Index
    public ActionResult Index() {
        this.DetectMobileDevices();
        if(this.IsMobile){
            return RedirectToAction("MobileIndex");
        } else {
            // actual action code goes here
            return View();
        }            
    }
}

Alternately you can use return View("ViewName"); but from my experience you want to actually perform different actions as opposed to just showing the result in a different view as in the case of presenting an HTML table as opposed to a Flex table to help iPhone users since there is no flash support in the iPhone, etc. (as of this writing)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜