2016-11-25 62 views
0

我想一個動作返回到其他控制器返回動作到另一個控制器

例子:我有2個controlers:

[Route("myurl"] 
public class HomeController : Controller 
{ 
    public ActionResult Action1() 
    { 
     if (...) 
      { 
       return Action2(); //working fine i will keep my route 
      } 
     else 
      { 
       return OtherController.Action3(); //Don't know how to do it here. 
      } 
    } 
    public ActionResult Action2() 
    { 
     return View(); 
    } 
} 

public class OtherController : Controller 
{ 
    public ActionResult Action3() 
    { 
     return View(); 
    } 
} 

它的工作,如果Action在同一個控制器中,但希望從HomeController將Action1從OtherController返回到Action3。 我想保持相同的路線(不是重定向到其他路線)。

有什麼想法?

回答

0

我發現:

我需要回到像即:

return DependencyResolver.Current.GetService<OtherController>().Action3(); 

[Route("myurl"] 
public class HomeController : Controller 
{ 
    public ActionResult Action1() 
    { 
     if (...) 
      { 
       return Action2(); //working fine i will keep my route 
      } 
     else 
      { 
       return DependencyResolver.Current.GetService<OtherController>().Action3(); 
      } 
    } 
    public ActionResult Action2() 
    { 
     return View(); 
    } 
} 
0

return RedirectToAction(「ActionName」,「ControllerName」);

+0

謝謝,但我想避免重定向 – Yanga

1

你試過用這個RedirectToAction

return RedirectToAction("ACTION_NAME", "CONTROLLER_NAME", new { area = "" }); 
+0

是的,我試過,但它做了重定向,我希望避免這種情況。 – Yanga

+0

Try this use: – prtdomingo

+0

'return View(「../ CONTROLLER_NAME/ACTION_NAME」);' – prtdomingo

0

你可以通過調用控制器類protected internal RedirectToRouteResult RedirectToAction(string actionName, string controllerName);的以下方法對您的要求做到這一點,否則塊中的代碼看起來像base.RedirectToAction("ACtion3","OtherController");

+0

我希望避免重定向並保留我的路由,如果在同一個控制器中返回一個操作,它就會工作。 – Yanga

相關問題