使用RedirectToAction传递模型和参数

6

我想将一个字符串和一个模型(对象)发送到另一个操作中。

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属性。
这是我的Search操作:
public ActionResult Search(string culture, HotelSearchModel hotelSearchModel)
{ 
    // ...
}
1个回答

13

使用 RedirectAction 无法发送数据。 这是因为您正在进行 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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接