0

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);
    }
}
  • Maybe edit the post and put some sample data up here? What is inside `EmployeeViewModel`? You might get your `webform` key wrong. – GaryNg Nov 22 '18 at 19:01
  • I found the answer. It is because, I am sending list of data to tempdata. And while retriving I am simply deserializing using object name. it should be public static List Get(this ITempDataDictionary tempData, string key) where T : class { object o; tempData.TryGetValue(key, out o); return o == null ? null : JsonConvert.DeserializeObject>((string)o); } – Deenadhayalan M Nov 23 '18 at 08:57

0 Answers0