Asp.Net MVC中Action跳转小结

首先我觉得action的跳转大致可以这样归一下类,跳转到同一控制器内的action和不同控制器内的action、带有参数的action跳转和不带参数的action跳转。

1、return View();//跳转到同方法名相同的本Controller下的视图

2、return RedirectToAction("Add");//跳转到本Controller下其他方法,不是返回视图

3、return RedirectToAction("Index", "Test");//跳转别的Controller方法

4、return RedirectToRoute(new { controller = "Test", action = "Index" });//跳转控制器下的Action

5、return RedirectToRoute(new { controller = "Test", action = "Index", id=1 });//可跳到其他controller,带参数。

public ActionResult Index(int id)
{
    ViewBag.ID = id;//Request["id"]这接收参数不行
    return View("MyView");
}

6、Response.Redirect("/Perpon/Add?id=1&name=hu");//本控制器下也需要写控制器名称

public ActionResult Add(string name)
{
    ViewData["id"] = Request["id"];//这种方式也能接收参数
    ViewData["name"] = name;
    return View();
}

7、return Redirect("/Perpon/Add?id=1&name=hu");//接收参数的方式与第6 相同

8、return View("Index"); //直接显示对应的页面 不经过执行Controller的方法。 

9、return View("/Views/Perpon/Add.cshtml");//这种方法是写全路径,直接显示页面,不经过Controller方法

原文地址:https://www.cnblogs.com/gygtech/p/8662536.html