簡體   English   中英

調用另一個控制器,並對調用的控制器操作MVC ASP.NET使用不同的布局

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

是MVC ASP.Net的新手。 我想從另一個控制器調用一個動作方法,並使用另一個布局頁面。

我的視圖/共享文件夾有兩個布局頁面:〜/ Views / Shared / _Layout1.cshtml和〜/ Views / Shared / _Layout2.cshtml。 下面是我的代碼:

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

 }

如果要使用其他布局,則可以使用View方法的重載版本:

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

如果要將控制流重定向到其他控制器/操作,則:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM