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?
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.
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.
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.
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);
}
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With