如何从另一个控制器重定向到索引?

cjo*_*136 118 c# asp.net-mvc

我一直在寻找一些方法来重定向到Index另一个控制器的视图.

public ActionResult Index()
{                
     ApplicationController viewModel = new ApplicationController();
     return RedirectToAction("Index", viewModel);
}
Run Code Online (Sandbox Code Playgroud)

这就是我现在尝试的.现在我给的代码有一个ActionLink链接到我需要的页面Redirect.

@Html.ActionLink("Bally Applications","../Application")
Run Code Online (Sandbox Code Playgroud)

mus*_*fan 254

使用带有控制器名称的重载...

return RedirectToAction("Index", "MyController");
Run Code Online (Sandbox Code Playgroud)

@Html.ActionLink("Link Name","Index", "MyController", null, null)
Run Code Online (Sandbox Code Playgroud)

  • 好的,这很有效.我之前尝试过这一点,当我之前做过这件事时,我一定是个错字. (3认同)
  • 那样做会更快,但有一个计时器阻止我 (2认同)

jav*_*iry 26

尝试:

public ActionResult Index() {
    return RedirectToAction("actionName");
    // or
    return RedirectToAction("actionName", "controllerName");
    // or
    return RedirectToAction("actionName", "controllerName", new {/* routeValues, for example: */ id = 5 });
}
Run Code Online (Sandbox Code Playgroud)

.cshtml视野中:

@Html.ActionLink("linkText","actionName")
Run Code Online (Sandbox Code Playgroud)

要么:

@Html.ActionLink("linkText","actionName","controllerName")
Run Code Online (Sandbox Code Playgroud)

要么:

@Html.ActionLink("linkText", "actionName", "controllerName", 
    new { /* routeValues forexample: id = 6 or leave blank or use null */ }, 
    new { /* htmlAttributes forexample: @class = "my-class" or leave blank or use null */ })
Run Code Online (Sandbox Code Playgroud)

注意事项使用null不建议在最终表达,是更好地使用空白new {}的,而不是null

  • 关于你的通知,为什么最好使用`new {}`而不是`null`? (3认同)

Wou*_*ort 15

您可以使用以下代码:

return RedirectToAction("Index", "Home");
Run Code Online (Sandbox Code Playgroud)

请参阅RedirectToAction