Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store complex object in TempData

I've been trying to pass data to an action after a redirect by using TempData like so:

if (!ModelState.IsValid) {     TempData["ErrorMessages"] = ModelState;     return RedirectToAction("Product", "ProductDetails", new { code = model.ProductCode }); } 

but unfortunately it's failing with the following message:

'System.InvalidOperationException The Microsoft.AspNet.Mvc.SessionStateTempDataProvider' cannot serialize an object of type 'ModelStateDictionary' to session state.'

I've found an issue in the MVC project in Github, but while it explains why I'm getting this error, I can't see what would be a viable alternative.

One option would be to serialize the object to a json string and then deserialize it back and reconstruct the ModelState. Is this the best approach? Are there any potential performance issues I need to take into account?

And finally, are there any alternatives for either serializing complex object or using some other pattern that doesn't involve using TempData?

like image 242
elolos Avatar asked Jan 06 '16 17:01

elolos


People also ask

Can TempData hold objects?

TempData is a dictionary object to store data temporarily. It is a TempDataDictionary class type and instance property of the Controller base class. TempData is able to keep data for the duration of a HTP request, in other words it can keep live data between two consecutive HTTP requests.

How do you persist data in TempData?

stringstr = TempData["MyData"]; Even if you are displaying, it's a normal read like the code below: @TempData["MyData"]; Condition 3 (Read and Keep): If you read the “TempData” and call the “Keep” method, it will be persisted. @TempData["MyData"]; TempData.

What is TempData how TempData it stores values?

TempData is used to transfer data from view to controller, controller to view, or from one action method to another action method of the same or a different controller. TempData stores the data temporarily and automatically removes it after retrieving a value. TempData is a property in the ControllerBase class.


1 Answers

You can create the extension methods like this:

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);     } } 

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<ClassA>("key") where value retrieved will be of type ClassA

like image 76
hem Avatar answered Oct 13 '22 06:10

hem