How to fix ViewState serialization error using my custom control?

I am writing my first asp.net user control.

Displays a list of locations that the user can select. I can load the list of elements into the control, and I can see what location the user chose when the page goes back, but the list itself is lost when the page returns.

I populate the list with an object of type IEnumerable<DetailedLocation>, where DetailedLocationis a class with several fields string. When I try to put a list in the ViewState, I get the following error:

Type 'System.Linq.OrderedEnumerable`2  
[[DetailedLocation, AT.Common, Version=1.0.0.0, Culture=neutral, 
PublicKeyToken=null],[System.String, mscorlib, Version=2.0.0.0,  
Culture=neutral, PublicKeyToken=b77a5c561934e089]]' in Assembly 'System.Core,  
Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not 
marked as serializable. 

Of course, my type can be marked as serializable, but this error message is saved. I know that it should be possible to do this work somehow. In the end, I can pass an object of this type to GridView, and the data will be saved after the postback.

How to fix it?

+3
source share
1 answer

If you have already marked your type as serializable, try saving the collection as List instead of IEnumerable. I assume that under the IEnumerable interface there is some type of concrete LINQ collection that is not serializable.

+3
source

Source: https://habr.com/ru/post/1791052/


All Articles