本文介绍了使用 RedirectToAction 传递模型和参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想向另一个动作发送一个字符串和一个模型(对象).
I want to send a string and a model (object) to another action.
var hSM = new HotelSearchModel();
hSM.CityID = CityID;
hSM.StartAt = StartAt;
hSM.EndAt = EndAt;
hSM.AdultCount = AdultCount;
hSM.ChildCount = ChildCount;
return RedirectToAction("Search", new { culture = culture, hotelSearchModel = hSM });
当我使用 new
关键字时,它会发送 null
对象,尽管我设置了对象的 hSm
属性.
When I use the new
keyword it sends null
object, although I set the objects hSm
property.
这是我的搜索
操作:
public ActionResult Search(string culture, HotelSearchModel hotelSearchModel)
{
// ...
}
推荐答案
您不能使用 RedirectAction
发送数据.那是因为您正在执行 301
重定向,然后返回到客户端.
You can't send data with a RedirectAction
.That's because you're doing a 301
redirection and that goes back to the client.
您需要将其保存在 TempData 中:
What you need to is save it in TempData:
var hSM = new HotelSearchModel();
hSM.CityID = CityID;
hSM.StartAt = StartAt;
hSM.EndAt = EndAt;
hSM.AdultCount = AdultCount;
hSM.ChildCount=ChildCount;
TempData["myObj"] = new { culture = culture,hotelSearchModel = hSM };
return RedirectToAction("Search");
之后,您可以再次从 TempData 中检索:
After that you can retrieve again from the TempData:
public ActionResult Search(string culture, HotelSearchModel hotelSearchModel)
{
var obj = TempData["myObj"];
hotelSearchModel = obj.hotelSearchModel;
culture = obj.culture;
}
这篇关于使用 RedirectToAction 传递模型和参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!