MVC 3 DropDownListFor:想不通'System.Web.Mvc.SelectList'不能被序列化 [英] MVC 3 DropDownListFor: Cannot figure out 'System.Web.Mvc.SelectList' cannot be serialized
问题描述
我不知道如何做一个DropDownList系列化,正在错误键入System.Web.Mvc.SelectList'不能被序列化。我使用的序列化向导形式通过坚持到底用户输入,然后发布一个确认。
I cannot figure out how to do serialization with a DropDownList and am getting the error "Type 'System.Web.Mvc.SelectList' cannot be serialized." I am using serialization in a wizard form to persist the user inputs through the end and then to post a confirmation.
我使用视图中的以下内容:
I am using the following in a view:
@using (Html.BeginFormAntiForgeryPost())
{
@Html.Hidden("myData", new MvcSerializer().Serialize(Model, SerializationMode.Signed))
...
@Html.DropDownListFor(m => m.RealEstate, Model.RealEstateList)
...
}
在我的视图模型(迈德特),我有:
In my ViewModel (MyData), I have:
[Serializable]
public class myData
{
public int RealEstate { get; set; }
public IEnumerable<SelectListItem> RealEstateList { get; set; }
...
public MyData()
{
var realestatelist = new List<SelectListItem>() {
new SelectListItem { Text = "(Please select)" },
new SelectListItem { Value = "1", Text="Some text." },
new SelectListItem { Value = "2", Text="Some other text." }
};
this.RealEstateList = new SelectList(realestatelist, "Value", "Text");
}
}
任何帮助是极大AP preciated。
Any help is greatly appreciated.
推荐答案
选择列表不支持序列。尝试使用的IEnumerable&LT; SelectedListItem&GT;
,的IList&LT; SelectListItem&GT;
或列表&LT; SelectedListItem&GT;
在您的控制器,然后在视图中创建选择列表。
The SelectList does not support serialization. Try to use IEnumerable<SelectedListItem>
, IList<SelectListItem>
or List<SelectedListItem>
in your controller and then create the SelectList in the view.
与此类似变化控制器:
public MyData()
{
var realestatelist = new List<SelectListItem>() {
new SelectListItem { Text = "(Please select)" },
new SelectListItem { Value = "1", Text="Some text." },
new SelectListItem { Value = "2", Text="Some other text." }
};
this.RealEstateList = realestatelist;
}
}
和看到你的 RealEstateList
在模型中已经是的IEnumerable&LT; SelectListItem&GT;
你可以在视图中选择列表那么无需转换模型属性像这样指定值和文本:
And seeing your RealEstateList
in the model is already IEnumerable<SelectListItem>
you can make the SelectList in the view then to specify the value and text without converting the model property like this:
@Html.DropDownListFor(m => m.RealEstate, new SelectList(Model.RealEstateList, "Value", "Text"))
这篇关于MVC 3 DropDownListFor:想不通'System.Web.Mvc.SelectList'不能被序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!