使用RedirectToAction传递模型和参数

use*_*401 6 parameters asp.net-mvc controller

我想将一个字符串和一个模型(对象)发送给另一个动作.

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 });
Run Code Online (Sandbox Code Playgroud)

当我使用new关键字时它发送null对象,虽然我设置了对象hSm属性.

这是我的Search行动:

public ActionResult Search(string culture, HotelSearchModel hotelSearchModel)
{ 
    // ...
}
Run Code Online (Sandbox Code Playgroud)

Ken*_*eth 13

你不能用a发送数据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");
Run Code Online (Sandbox Code Playgroud)

之后,您可以从TempData中再次检索:

public ActionResult Search(string culture, HotelSearchModel hotelSearchModel)
{
    var obj = TempData["myObj"];
    hotelSearchModel = obj.hotelSearchModel;
    culture = obj.culture;
}
Run Code Online (Sandbox Code Playgroud)