Passing Model and Parameter Using RedirectToAction

I want to send a row 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 }); 

When I use the new keyword, it dispatches a null object, although I set the hSm property of the objects.

This is my Search action:

 public ActionResult Search(string culture, HotelSearchModel hotelSearchModel) { // ... } 
+6
source share
1 answer

You cannot send data using RedirectAction . This is because you are doing a 301 redirect and returning to the client.

What you need is to 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"); 

After that, you can get again from TempData:

 public ActionResult Search(string culture, HotelSearchModel hotelSearchModel) { var obj = TempData["myObj"]; hotelSearchModel = obj.hotelSearchModel; culture = obj.culture; } 
+13
source

Source: https://habr.com/ru/post/944651/


All Articles