使用過ASP.NET MVC的人都知道在MVC中頁面後臺中常用的頁面跳轉方法有幾種,如:return View()、return RedirectToAction()
一般情況下我們返回的都是本頁面,所以使用return View()就可以解決問題了,但是很多時候我們也會遇到返回的頁面不是本頁面的,那麼就會用到後面兩種,但是如果我們在頁面返回的時候也要返回操作的結果的時候,我們就要對return RedirectToAction();方法進行擴展使用了。
RedirectToAction(string ActionName);
RedirectToAction(string ActionName, object viewData);
RedirectToAction(string ActionName, string ControllerName);
RedirectToAction(string ActionName, string ControllerName, object viewData);
舉例使用:
1、跳轉到同一Controller 裏面的不同Action,如:HomeController裏面的不同頁面跳轉到Index頁面,RedirectToAction("Index");
2、跳轉到同一Controller 裏面的不同Action,含參數,如:HomeController裏面的不同頁面跳轉到Index頁面,並傳遞msg="操作成功",RedirectToAction("Index",new {msg="操作成功"});
3、跳轉到不同Controller 裏面的不同Action,如:其他Controller裏面的頁面跳轉到HomeController裏面的Index頁面,RedirectToAction("Index","Home");
2、跳轉到不同Controller 裏面的不同Action,含參數,如:其他Controller裏面的頁面跳轉到HomeController裏面的Index頁面,並傳遞msg="操作成功",RedirectToAction("Index","Home",new {msg="操作成功"});
傳遞多個參數類似:
RedirectToAction("Index","Home",new {msg="操作成功",name="admin"});
當然要使用傳遞參數的話參數名是要在Global.asax裏面配置的,別忘記了。