Passing a variable from the [HttpPost] method to the [HttpGet] method

I redirect the view from the [HttpPost] method to the [HttpGet] method. I got it to work, but I want to know if this is the best way to do this.

Here is my code:

[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(); } 

View:

 <%@ 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> 
+4
source share
4 answers

ASP.NET MVC has basically 3 methods for implementing a PRG template.

  • TempData STRONG>

Using TempData indeed one way to transfer information for a single redirect. The drawback that I see with this approach is that if the user presses F5 on the last redirected page, he will no longer be able to retrieve the data, since he will be removed from TempData for subsequent requests:

 [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); } 
  • Query String Parameters

Another approach, if you do not have a large amount of data to send, is to send them as query string parameters, for example:

 [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); } 
  • Constancy

Another approach and IMHO the best thing is to save this model in some kind of data store (for example, in a database or something else, and then when you want to redirect the GET action, send only the identifier that allows it to choose the model from anywhere saved it). Here's the pattern:

 [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); } 

Each method has its pros and cons. You can choose which one is best for your scenario.

+8
source

If you insert this data into the database, you should redirect them to the action of the controller that has this data on the route:

 /Students/View/1 

Then you can write code in the controller to retrieve data from the database for display:

 public ActionResult View(int id) { // retrieve from the database // create your view model return View(model); } 
+2
source

One of the overrides of RedirectToAction() as follows:

 RedirectToAction(string actionName, object routeValues) 

You can use this as:

 [HttpPost] public ActionResult SubmitStudent() { StudentViewModel model = TempData["model"] as StudentResponseViewModel; return RedirectToAction("DisplayStudent", new {id = model.ID, name = model.Name}); } [HttpGet] public ActionResult DisplayStudent(string id, string name) { ViewData["id"] = TempData["id"]; ViewData["name"] = TempData["name"]; return View(); } 

Hope this works.

0
source

This is the classic Post-Redirect-Get (PRG) template , and it looks great, but I would add one bit of code. In the DisplayStudent method, check if your TempData variables are invalid, otherwise redirect to some default index action. This is in case the user presses F5 to refresh the page.

 public ActionResult DisplayStudent() { if(TempData["model"] == null) { return RedirectToAction("Index"); } var model = (StudentResponseViewModel)TempData["model"]; return View(model); } public ViewResult Index() { IEnumerable<StudentResponseViewModel> students = GetAllStudents(); return View(students); } 
0
source

All Articles