0

On page Index i have some form. On submit I use this code in HomeController:

    [HttpPost]
    public ActionResult Index(EventDetails obj)
    {

        if (ModelState.IsValid)
        {
            return RedirectToAction("Index2","Home");
        }
        else
        {
            return View();
        }

    }

    public ActionResult Index2()
    {

        return View();
    }

So it will redirect me to another page named Index2. How can i get POST data, sent in "Index" page and use it in "Index2" page. And how to show POST data, sent by prev. page in view page?

dantey89
  • 2,167
  • 24
  • 37
  • You could just use pass the post data as routeValues when redirecting... – Daniel Conde Marin Oct 30 '14 at 23:05
  • 1
    please see this answer http://stackoverflow.com/questions/129335/how-do-you-redirect-to-a-page-using-the-post-verb – Oscar Cabrero Oct 30 '14 at 23:09
  • If `EventDetails` contains only primitive properties the you can use `return RedirectToAction("Index2", obj);` and change `public ActionResult Index2(EventDetails obj) {..` (note wont work if any properties are complex objects or collections). Alternatively you can add `obj` to session and retrieve it in `Index2`. –  Oct 30 '14 at 23:13

1 Answers1

2

Since you're making a GET request after the POST, you cannot send the body from the POST along with it. The easiest fix is to use TempData, to temporarily store the data across requests:

[HttpPost]
public ActionResult Index(EventDetails obj)
{

    if (ModelState.IsValid)
    {
        TempData["eventDetails"] = obj;
        return RedirectToAction("Index2","Home");
    }
    else
    {
        return View();
    }

}

public ActionResult Index2()
{
    var obj = TempData["eventDetails"] as EventDetails;
    return View(obj);
}
Tobias
  • 2,811
  • 2
  • 20
  • 31