passing model and parameter with RedirectToAction

17,774

You can't send data with a RedirectAction. That's because you're doing a 301 redirection and that goes back to the client.

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");

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;
}
Share:
17,774
user1688401
Author by

user1688401

Updated on June 12, 2022

Comments

  • user1688401
    user1688401 almost 2 years

    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 });
    

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

    This is my Search action :

    public ActionResult Search(string culture, HotelSearchModel hotelSearchModel)
    { 
        // ...
    }