Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a model object to a RedirectToAction without polluting the URL?

Here's what I'm trying to do:

public ActionResult Index()
{
    return View();
}

[HttpPost]
public ActionResult Index(ContactModel model)
{
    if (ModelState.IsValid)
    {
        // Send email using Model information.

        return RedirectToAction("Gracias", model);
    }

    return View(model);
}

public ActionResult Gracias(ContactModel model)
{
    return View(model);
}

All three action methods are in the same controller. Basically, a user type up some data in the contact form and I want to redirect them to a thank you page using their name in the Model object.

As the code is, it works, but the URL passed along with GET variables. Not ideal.

http://localhost:7807/Contacto/Gracias?Nombre=Sergio&Apellidos=Tapia&Correo=opiasdf&Telefono=oinqwef&Direccion=oinqef&Pais=oinqwef&Mensaje=oinqwef

Any suggestions?

like image 566
sergserg Avatar asked Oct 22 '12 21:10

sergserg


People also ask

Can we pass model in RedirectToAction?

Finally, the PersonModel class object is passed to the RedirectToAction method along with the name of the destination Controller and its Action method. The Controller consists of the following Action method. Inside this Action method, the PersonModel class object is received.

How do you pass an object in redirect to action?

You can not pass classes to RedirectToAction method, if you want to pass an entire object in a querystring or via POST you can serialize the object using XML or JSON and deserialize the object in the receiver controller. If you use this approach to be careful on the size of the object serialized.

What is difference between redirect and RedirectToAction?

RedirectToAction is meant for doing 302 redirects within your application and gives you an easier way to work with your route table. Redirect is meant for doing 302 redirects to everything else, specifically external URLs, but you can still redirect within your application, you just have to construct the URLs yourself.


2 Answers

Sounds like a solution for TempData!

[HttpPost]
public ActionResult Index(ContactModel model)
{
  if (ModelState.IsValid)
  {
    // Send email using Model information.
    TempData["model"] = model;
    return RedirectToAction("Gracias");
  }

  return View(model);
}

public ActionResult Gracias()
{
  ContactModel model = (ContactModel)TempData["model"];
  return View(model);
}
like image 108
Erik Philips Avatar answered Sep 29 '22 20:09

Erik Philips


Instead of doing

return RedirectToAction("Gracias", model);

You could do

[HttpPost]
public ActionResult Index(ContactModel model)
{
    if (ModelState.IsValid)
    {
        // Send email using Model information.

        return View("Gracias", model);
    }

    return View(model);
}

and remove your Gracias controller action. Using above the "Gracias" view will be displayed with your ContactModel model.

I don't see the need to have a separate controller action if it uses the same model and is a lock step part of the workflow ex. "a successful POST to Index will always result in the Gracias View being displayed"

You could also store the model in TempData (which is like a 1 request session state) but I don't see any point in doing that in your situation as it just complicates things

Thoughts?

like image 23
John Culviner Avatar answered Sep 29 '22 19:09

John Culviner