简体   繁体   中英

Call another controller and use a different Layout for the called controller action MVC ASP.NET

Am new to MVC ASP.Net. I would like to call an action method from another controller and use a different layout page.

My Views/Shared folder has two layout pages: ~/Views/Shared/_Layout1.cshtml and ~/Views/Shared/_Layout2.cshtml. Below is my code:

//HomeController
public class HomeController : Controller
{
    public ActionResult Index(string id)
    {
        if(String.IsNullOrEmpty(id))
        {
          //Call someMethod from UserController
          //And when called, it should a different Layout page: ~/Views/Shared/_Layout2.cshtml
          return View("someMethod"); 
        }
        else
        {
          return AboutPage(id); //uses ~/Views/Shared/_Layout1.cshtml
        }
     }
    public ActionResult AboutPage(string id)
    {
       return View();
    }

 }

If you want to use different layout then you can use overloaded version of View method:

return View("view", "_Layout1"); 

If you want to redirect control flow to different controller/action then:

return RedirectToAction("action", "controller");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM