传递一个模型对象到RedirectToAction不污染的网址是什么? [英] Passing a model object to a RedirectToAction without polluting the URL?
本文介绍了传递一个模型对象到RedirectToAction不污染的网址是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面就是我想要做的:
公众的ActionResult指数()
{
返回查看();
}[HttpPost]
公众的ActionResult指数(ContactModel模型)
{
如果(ModelState.IsValid)
{
//使用型号信息发送电子邮件。 返回RedirectToAction(格拉西亚斯模型);
} 返回查看(模型);
}公众的ActionResult格拉西亚斯(ContactModel模型)
{
返回查看(模型);
}
所有三项行动方法都在同一个控制器。基本上,用户键入了联系表的一些数据,我想在Model对象使用他们的名字将其重定向到一个感谢您网页。
由于code是,它的工作原理,但URL以GET变量传承下去。不理想。
<$p$p><$c$c>http://localhost:7807/Contacto/Gracias?Nombre=Sergio&Apellidos=Tapia&Correo=opiasdf&Telefono=oinqwef&Direccion=oinqef&Pais=oinqwef&Mensaje=oinqwef有什么建议?
解决方案
听起来像是的 TempData的!
[HttpPost]
公众的ActionResult指数(ContactModel模型)
{
如果(ModelState.IsValid)
{
//使用型号信息发送电子邮件。
TempData的[模型] =模型;
返回RedirectToAction(格拉西亚斯);
} 返回查看(模型);
}公众的ActionResult格拉西亚斯()
{
ContactModel模型=(ContactModel)的TempData [模型];
返回查看(模型);
}
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?
解决方案
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);
}
这篇关于传递一个模型对象到RedirectToAction不污染的网址是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文