Search code examples
asp.net-mvclistviewrazorcontrollerigrouping

Redirect IGrouping<string, model> List to another action in Same Controller


return RedirectToAction("ActionName", new { lst = finalData });
[HttpGet]
 Public ActionResult AcionName(IGrouping<string, ModelName> lst)
 {
  return View("ActionName", lst);
 }

i use this code to redirect my list to another action but this is not working.


Solution

  • You can assign the finalData to a Session or TempData variable.

    TempData["FinalData "] = finalData;
    return RedirectToAction("ActionName");
    

    From this answer: "TempData Allows you to store data that will survive for a redirect. Internally it uses the Session, it's just that after the redirect is made the data is automatically evicted"

    Then in your GET Action Method,

    Public ActionResult AcionName()
    {
        var finalData = TempData["FinalData"] as IGrouping<string, ModelName>;
        return View("ActionName", finalData);
    }
    

    The problem is, if you were to refresh after the redirect, then finalData would be null. So, in that case you use Session["FinalData"] or get the data from the Database in your Get method again. You can go through the answer I have linked for disadvantages of using TempData.