将变量从[HttpPost]方法传递给[HttpGet]方法

use*_*468 4 asp.net-mvc http-get http-post http-redirect

我正在将视图从[HttpPost]方法重定向到[HttpGet]方法.我已经开始工作,但想知道这是否是最好的方法.

这是我的代码:

[HttpPost] 
public ActionResult SubmitStudent()
{ 
StudentViewModel model = TempData["model"] as StudentResponseViewModel; 

TempData["id"] = model.Id; 
TempData["name"] = model.Name; 

return RedirectToAction("DisplayStudent"); 
}

[HttpGet] 
public ActionResult DisplayStudent() 
{ 
ViewData["id"] = TempData["id"]; 
ViewData["name"] = TempData["name"]; 

return View(); 
}
Run Code Online (Sandbox Code Playgroud)

视图:

<%@ Page 
Language="C#"
Inherits="System.Web.Mvc.ViewPage"
 %> 
<html>
 <head runat="server"> 
<title>DisplayStudent</title> 
</head> 
<body> 
<div> 
<%= ViewData["id"]%> <br /> 
<%= ViewData["name"]%> 
</div> 
</body> 
</html>
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 8

ASP.NET MVC中基本上有3种技术来实现PRG模式.

  • TempData的

使用TempData确实是传递单个重定向信息的一种方法.我用这种方法看到的缺点是,如果用户点击最后的重定向页面上F5他将不再能够获取数据,因为它会从被删除TempData后续请求:

[HttpPost] 
public ActionResult SubmitStudent(StudentResponseViewModel model)
{ 
    if (!ModelState.IsValid)
    {
        // The user did some mistakes when filling the form => redisplay it
        return View(model);
    }

    // TODO: the model is valid => do some processing on it

    TempData["model"] = model;
    return RedirectToAction("DisplayStudent");
}

[HttpGet] 
public ActionResult DisplayStudent() 
{ 
    var model = TempData["model"] as StudentResponseViewModel;
    return View(model); 
}
Run Code Online (Sandbox Code Playgroud)
  • 查询字符串参数

如果您没有很多要发送的数据,另一种方法是将它们作为查询字符串参数发送,如下所示:

[HttpPost] 
public ActionResult SubmitStudent(StudentResponseViewModel model)
{ 
    if (!ModelState.IsValid)
    {
        // The user did some mistakes when filling the form => redisplay it
        return View(model);
    }

    // TODO: the model is valid => do some processing on it

    // redirect by passing the properties of the model as query string parameters
    return RedirectToAction("DisplayStudent", new 
    {
        Id = model.Id,
        Name = model.Name
    });
}

[HttpGet] 
public ActionResult DisplayStudent(StudentResponseViewModel model) 
{ 
    return View(model); 
}
Run Code Online (Sandbox Code Playgroud)
  • 坚持

另一种方法和恕我直言最好的方法是将这个模型保存到一些数据存储(如数据库或其他东西,然后当你想重定向到GET操作时只发送一个id,允许它从你持久化的地方获取模型) .这是模式:

[HttpPost] 
public ActionResult SubmitStudent(StudentResponseViewModel model)
{ 
    if (!ModelState.IsValid)
    {
        // The user did some mistakes when filling the form => redisplay it
        return View(model);
    }

    // TODO: the model is valid => do some processing on it

    // persist the model
    int id = PersistTheModel(model);

    // redirect by passing the properties of the model as query string parameters
    return RedirectToAction("DisplayStudent", new { Id = id });
}

[HttpGet] 
public ActionResult DisplayStudent(int id) 
{ 
    StudentResponseViewModel model = FetchTheModelFromSomewhere(id);
    return View(model); 
}
Run Code Online (Sandbox Code Playgroud)

每种方法都有其优点和缺点.由您决定哪一个最适合您的场景.