I need to store my IEnumerable of objects into Tempdata for my next request from the server. Since Tempdata cannot store Objects I found the below extenstion method to serialize and deserialize before putting into Tempdata. After this method, I am able to store my object to Tempdata. But, when I am trying to use it in my next action method, I am facing following runtime error. "Cannot deserialize the current JSON array". Please help me to fix this issue.
my code :
public ActionResult GetData(){
List<EmployeeViewModel> webform =
_service.GetListOfWebformData(id).ToList();
TempData.Put("webform", webform); }
public ActionResult ShowData(){
var WebformData = TempData.Get<EmployeeViewModel>("webform");
return View(WebformData);}
Extension method i have used for Tempdata is below
public static class TempDataExtensions
{
public static void Put<T>(this ITempDataDictionary tempData, string key, T value) where T : class
{
tempData[key] = JsonConvert.SerializeObject(value);
}
public static T Get<T>(this ITempDataDictionary tempData, string key) where T : class
{
object o;
tempData.TryGetValue(key, out o);
return o == null ? null : JsonConvert.DeserializeObject<T>((string)o);
}
}
>((string)o); }