繁体   English   中英

Mvc3 C# - 是否可以从不同的控制器调用动作?

[英]Mvc3 C# - Is it possible to call an action from a different controller?

在我的项目中,我有两个不同的控制器。
这是主要的一个:

public class Main : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

这是另一个:

public class OtherOne : Controller
{
    public ActionResult RandomAction()
    {
        return ... //more code here
    }
}

我应该在“OtherOne / RandomAction”中返回什么才能获得“主要/索引”操作的相同结果?

这很简单:

public class OtherOneController : Controller
{
    public ActionResult RandomAction()
    {
        return RedirectToAction("Index", "Main");
    }
}

您的Controller类名必须是MainController和OtherOneController。 如果要更改它,请查看以下文章: 在ASP.NET MVC中更改控制器名称约定

这是您的主控制器:

public class MainController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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