Haroon nasir
Haroon nasir

Reputation: 676

Serialization error occurs when using TempData in my action

I am trying to use tempdata to pass data to another action method, but the following error occurs when the page is rendered:

Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode.

This is my initial action

 public ActionResult Request(string id)
 {
     viewmodelservice = obj = new viewmodelservice ();
     obj.list = getlist(); 
     TempData["VmList"] = obj.list;
     return View(requestBackUpScheduleVMViewModelObj);
 }

And here is the additional action where I am retrieving the value from TempData

 public ActionResult getRequest(string id,[DataSourceRequest] DataSourceRequest request)
 {
     viewmodelservice = obj = new viewmodelservice ();
     obj = (viewmodelservice )TempData["VmList"]
     return json(obj.ToDataSourceResult(request));
 }

How can I resolve this error?

Upvotes: 0

Views: 944

Answers (1)

ste-fu
ste-fu

Reputation: 7482

You need to mark the class you are serializaing as [Serializable] (and make sure that it is...). That is whatever class obj.getlist returns.

If obj.getlist() returns a List<MyClass> then MyClass needs to have the attribute applied

TempData uses session under the hood, and if you are using a remote/shared session state eg Redis or SqlServer then all items that you store in session will need to be marked so.

Upvotes: 2

Related Questions