I needed to redirect to different action method after some work in another action method. I did use RedirectToAction but .NET Core MVC application was only displaying blank.
Scenario was:
public ActionResult Method1()
{
..........
..........
var temp = (List<object>)TempData["KeyName"];
}
public ActionResult Method2()
{
..............
List<object> obj = {my list of object};
TempData["keyName"] = obj;
RedirectToAction("Method1")
}
However, application displayed blank page and in URL it was like this "http://localhost/{controller name}/Method2
So, what was the problem? Problem was not in RedirectToAction method. Problem was in the way I assigned data to TempData. To fix the problem, I had to serialize the data before assigning the data to TempData.
Solution for the above scenario:
public ActionResult Method1()
{
..........
..........
var temp = JsonConvert.DeserializeObject<List<object>>(TempData["KeyName"].ToString());
}
public ActionResult Method2()
{
..............
List<object> obj = {my list of object};
TempData["keyName"] = JsonConvert.SerializeObject(obj);
RedirectToAction("Method1")
}
Thank you for reading this blog. Hope this is helpful.
Scenario was:
public ActionResult Method1()
{
..........
..........
var temp = (List<object>)TempData["KeyName"];
}
public ActionResult Method2()
{
..............
List<object> obj = {my list of object};
TempData["keyName"] = obj;
RedirectToAction("Method1")
}
However, application displayed blank page and in URL it was like this "http://localhost/{controller name}/Method2
So, what was the problem? Problem was not in RedirectToAction method. Problem was in the way I assigned data to TempData. To fix the problem, I had to serialize the data before assigning the data to TempData.
Solution for the above scenario:
public ActionResult Method1()
{
..........
..........
var temp = JsonConvert.DeserializeObject<List<object>>(TempData["KeyName"].ToString());
}
public ActionResult Method2()
{
..............
List<object> obj = {my list of object};
TempData["keyName"] = JsonConvert.SerializeObject(obj);
RedirectToAction("Method1")
}
Thank you for reading this blog. Hope this is helpful.
Comments
Post a Comment