ASP.NET MVC3 - How to Serve View () from Another Controller

So, to accomplish what I asked in this , 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. Although I can see that it is executing the Test.Areas.Mobile.Controllers.HomeController().Index() action method is just fine. What's going on here? And how can I show the Index view from the Mobile area without changing the request URL (as indicated in the original post mentioned above)?

+4
source share
3 answers

You have several options:

  • Redirecting to the action you want to return: return RedirectToAction("Action-I-Want") .
  • Return the view by name: return View("The-View-I-Want") .

Note that in the second approach, you will need to place your view in the Shared folder so that all controllers can find and return it. This can become erratic if you end up posting all of your views.

As an additional note: the reason your work does not find a view is because the view engine, by default, looks for a view in a folder that β€œbelongs” to the current context of the executive controller, regardless of what code you call.

Edit:

You can group all the "mobile" views in one folder. On your Global.asax server (or when you set up your ViewEngine , just add the path to your mobile view in AreaViewLocationFormats . Keep in mind that you still have to name your views differently.

You can also write your own viewing engine. I would do something like detecting the browser and then serving the desired file. You can customize the convention such as View.aspx and View.m.aspx.

Anyway, just take a look at WebFormViewEngine and you will understand what works best for you.

+4
source

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

There are View() overloads, which can also pass route information, but it takes more effort to create them.

+1
source

Actually the easiest way is to make one version of your site programmed to standards, and not to detect browsers: D - however, in the direct answer, to accomplish what it is in most of the ASP.NET mvc model, using:

 RedirectToAction("ViewName", "ControllerName"); 

- a good method, but I think it is more practical if you think that you need to program different browser standards to create the main view and an alternative "mobile" view under your controller views. Then, instead of writing special code on each controller, expand the controller instead.

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

Then change your controller classes, instead say the ControllerExtended classes and just add one line at the beginning of each action, you have alternative views, for example:

 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(); } } } 

Alternatively, you can use return View ("ViewName"); but in my experience, you really want to perform different actions, and not just show the result in a different view, as in the case of the presentation of the HTML table, and not the Flex table, to help iPhone users, since the iPhone does not have flash memory, etc. (starting with this letter)

0
source

Source: https://habr.com/ru/post/1347410/


All Articles