I\'ve been trying to pass data to an action after a redirect by using TempData like so:
if (!ModelState.IsValid)
{
TempData[\"ErrorMessages\"] = ModelSta
You can create the extension methods like this:
public static class TempDataExtensions
{
public static void Put(this ITempDataDictionary tempData, string key, T value) where T : class
{
tempData[key] = JsonConvert.SerializeObject(value);
}
public static T Get(this ITempDataDictionary tempData, string key) where T : class
{
object o;
tempData.TryGetValue(key, out o);
return o == null ? null : JsonConvert.DeserializeObject((string)o);
}
}
And, you can use them as follows:
Say objectA is of type ClassA. You can add this to the temp data dictionary using the above mentioned extension method like this:
TempData.Put("key", objectA);
And to retrieve it you can do this:
var value = TempData.Get
where value retrieved will be of type ClassA