当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Unable to serialize List<SelectListItem>

问题描述:

I'm moving an existing MVC3 app from InProc to ASP.Net Session State service.

One of the model objects has this property:

public List<System.Web.Mvc.SelectListItem> StateCodes { get; set; }

and it's throwing this error

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.

Is there a way to get List<SelectListItem> to serialize?

网友答案:

Try to create the select list items in the Razor code instead by with a SelectList. Let the model instead have a property for the underlying data that the SelectList is based on. You may also try to place the SelectList in the ViewBag or ViewData, that could also solve problem.

Storing SelectList's in the view model is regarded as bad practice.

网友答案:

[Just adding some information to the answer above] I believe that

Storing SelectList's in the view model is regarded as bad practice.

is due to the fact that the viewmodel shouldn't be builded in such a specific way about how the information will be shown to the final user.

If the viewmodel has a list of items, and the view renders a SelectList is fine of course. But the view could choose to present that data to the final user in many other ways.

分享给朋友:
您可能感兴趣的文章:
随机阅读: