我想将一个字符串和一个模型(对象)发送给另一个动作.
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属性.
这是我的搜索操作:
public ActionResult Search(string culture,HotelSearchModel hotelSearchModel) { // ... }
解决方法
您无法使用RedirectAction发送数据.
那是因为你正在进行301重定向,然后回到客户端.
那是因为你正在进行301重定向,然后回到客户端.
你需要的是将它保存在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中再次检索:
public ActionResult Search(string culture,HotelSearchModel hotelSearchModel) { var obj = TempData["myObj"]; hotelSearchModel = obj.hotelSearchModel; culture = obj.culture; }