Asp.net mvc 4 @HhtmlDropdown引发序列化异常异常

Asp.net mvc 4 @HhtmlDropdown引发序列化异常异常,asp.net-mvc-4,razor,serialization,html.dropdownlistfor,Asp.net Mvc 4,Razor,Serialization,Html.dropdownlistfor,我有一个表格有一个下拉列表。我正在使用SQLServer状态模式在SQLServer中保存会话。表单显示正确;但是,它在表单提交期间引发异常。我一直在寻找并尝试一些在网上发布的建议答案,但运气不佳。我做错了什么 //This is the section where I am building the SelectList Item in the controller var ListDepartment = ManageUsersMembership.GetDepartment(Sessio

我有一个表格有一个下拉列表。我正在使用SQLServer状态模式在SQLServer中保存会话。表单显示正确;但是,它在表单提交期间引发异常。我一直在寻找并尝试一些在网上发布的建议答案,但运气不佳。我做错了什么

//This is the section where I am building the SelectList Item in the controller

var ListDepartment = ManageUsersMembership.GetDepartment(SessionWrapper.studentBookTrade.SelectedSchoolID);
                // book.DepartmentList = ListDepartment; //new SelectList(ListDepartment, "DepartmentID", "DepartmentName", SessionWrapper.studentBookTrade.SelectedDepartmentID);
                List<SelectListItem> lDepartment = new List<SelectListItem>();
                foreach (var item in ListDepartment)
                {
                    if (SessionWrapper.studentBookTrade.SelectedDepartmentID > 0 && item.DepartmentID == SessionWrapper.studentBookTrade.SelectedDepartmentID)
                    {
                        lDepartment.Add(new SelectListItem { Text = item.DepartmentName, Value = item.DepartmentID.ToString(), Selected = true });
                    }
                    else
                    {
                        lDepartment.Add(new SelectListItem { Text = item.DepartmentName, Value = item.DepartmentID.ToString() });
                    }
                }
                book.DepartmentList = lDepartment;



//this is my property in the Model:

 public virtual IEnumerable<SelectListItem> DepartmentList { get; set; }

//This is the HTML Code in my View:

 @Html.DropDownListFor(model => model.DepartmentID, new SelectList(Model.DepartmentList, "Value", "Text"), "Select Department", new {@class="form-control", id="DepartmentID", onchange = "ValueSelected()" })
                 @Html.ValidationMessageFor(model => model.DepartmentID)



 //The exception

 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.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: 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.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[SerializationException: Type 'System.Web.Mvc.SelectList' in Assembly 'System.Web.Mvc, Version=4.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable.]
   System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type) +10751795
   System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) +230
   System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() +121
   System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Type objectType, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, SerializationBinder binder) +185
   System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo) +565
   System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck) +446
   System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck) +131
   System.Web.Util.AltSerialization.WriteValueToStream(Object value, BinaryWriter writer) +1666

[HttpException (0x80004005): 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.]
   System.Web.Util.AltSerialization.WriteValueToStream(Object value, BinaryWriter writer) +1754
   System.Web.SessionState.SessionStateItemCollection.WriteValueToStreamWithAssert(Object value, BinaryWriter writer) +34
   System.Web.SessionState.SessionStateItemCollection.Serialize(BinaryWriter writer) +628
   System.Web.SessionState.SessionStateUtility.Serialize(SessionStateStoreData item, Stream stream) +240
   System.Web.SessionState.SessionStateUtility.SerializeStoreData(SessionStateStoreData item, Int32 initialStreamSize, Byte[]& buf, Int32& length, Boolean compressionEnabled) +62
   System.Web.SessionState.SqlSessionStateStore.SetAndReleaseItemExclusive(HttpContext context, String id, SessionStateStoreData item, Object lockId, Boolean newItem) +135
   System.Web.SessionState.SessionStateModule.OnReleaseState(Object source, EventArgs eventArgs) +565
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69
//这是我在控制器中构建SelectList项的部分
var ListDepartment=ManageUsersMembership.GetDepartment(SessionWrapper.studentBookTrade.SelectedSchoolID);
//book.DepartmentList=列表部门//新的SelectList(ListDepartment,“DepartmentID”,“DepartmentName”,SessionWrapper.studentBookTrade.SelectedDepartmentID);
List lDepartment=新列表();
foreach(ListDepartment中的var项)
{
如果(SessionWrapper.studentBookTrade.SelectedDepartmentID>0&&item.DepartmentID==SessionWrapper.studentBookTrade.SelectedDepartmentID)
{
添加(新SelectListItem{Text=item.DepartmentName,Value=item.DepartmentID.ToString(),Selected=true});
}
其他的
{
添加(新SelectListItem{Text=item.DepartmentName,Value=item.DepartmentID.ToString()});
}
}
book.DepartmentList=lDepartment;
//这是我在模型中的属性:
公共虚拟IEnumerable部门列表{get;set;}
//这是我视图中的HTML代码:
@Html.DropDownListFor(model=>model.DepartmentID,新选择列表(model.DepartmentList,“Value”,“Text”),“Select Department”,新{@class=“form control”,id=“DepartmentID”,onchange=“ValueSelected()”})
@Html.ValidationMessageFor(model=>model.DepartmentID)
//例外
无法序列化会话状态。在“StateServer”和“SQLServer”模式下,ASP.NET将序列化会话状态对象,因此不允许使用不可序列化的对象或MarshalByRef对象。如果自定义会话状态存储在“自定义”模式下完成了类似的序列化,则同样的限制也适用。
描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。
异常详细信息:System.Web.HttpException:无法序列化会话状态。在“StateServer”和“SQLServer”模式下,ASP.NET将序列化会话状态对象,因此不允许使用不可序列化的对象或MarshalByRef对象。如果自定义会话状态存储在“自定义”模式下完成了类似的序列化,则同样的限制也适用。
源错误:
在执行当前web请求期间生成了未经处理的异常。有关异常的起源和位置的信息可以使用下面的异常堆栈跟踪来识别。
堆栈跟踪:
[SerializationException:程序集'System.Web.Mvc,版本=4.0.0.1,区域性=中性,PublicKeyToken=31bf3856ad364e35'中的类型'System.Web.Mvc.SelectList'未标记为可序列化。]
System.Runtime.Serialization.FormatterServices.InternalGetSerializationMembers(RuntimeType类型)+10751795
System.Runtime.Serialization.FormatterServices.GetSerializableMembers(类型类型,StreamingContext上下文)+230
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()+121
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerializate(类型objectType、ISurrogateSelector代理选择器、StreamingContext上下文、SeroObjectInfo init SeroObjectInfo init、IFormatterConverter转换器、SerializationBinder活页夹)+185
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo,NameInfo memberNameInfo,NameInfo typeNameInfo)+565
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serializate(对象图,头[]inHeaders,u BinaryWriter serWriter,Boolean fCheck)+446
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialization(流序列化流,对象图,头[]头,布尔fCheck)+131
System.Web.Util.AltSerialization.WriteValueToStream(对象值,BinaryWriter-writer)+1666
[HttpException(0x80004005):无法序列化会话状态。在“StateServer”和“SQLServer”模式下,ASP.NET将序列化会话状态对象,因此不允许使用不可序列化的对象或MarshalByRef对象。如果自定义会话状态存储在“自定义”模式下完成类似序列化,则同样的限制也适用。]
System.Web.Util.AltSerialization.WriteValueToStream(对象值,BinaryWriter-writer)+1754
System.Web.SessionState.SessionStateItemCollection.WriteValueToStreamWithAssert(对象值,二进制编写器)+34
System.Web.SessionState.SessionStateItemCollection.Serialize(BinaryWriter)+628
System.Web.SessionState.SessionStateUtility.Serialize(SessionStateStoreData项,流)+240
System.Web.SessionState.SessionStateUtility.SerializeStoreData(SessionStateStoreData项,Int32 initialStreamSize,字节[]&buf,Int32&length,布尔压缩启用)+62
System.Web.SessionState.SqlSessionStateStore.SetAndReleaseItemExclusive(HttpContext上下文、字符串id、SessionStateStoreData项、对象锁定id、布尔新项)+135
System.Web.SessionState.SessionStateModule.OnReleaseState(对象源,EventArgs EventArgs)+565
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+136
System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&同步完成)+69

我想你这里的东西太多了。尝试以这种方式更改代码

public IEnumerable<SelectListItem> DepartmentList { get; set; }

“for”助手将为您映射列表。看看这是否有帮助

我仍然遇到异常。但是,我添加了一个序列化部分
@Html.DropDownListFor(model => model.DepartmentID, Model.DepartmentList, "Select Department", new {@class="form-control", id="DepartmentID", onchange = "ValueSelected()" })