将默认值设置为选择列表

时间:2013-07-12 16:14:06

标签: c# asp.net-mvc html.dropdownlistfor selectlist

我有这个字符串列表:

public static readonly List<string> LIST_ITEM_STATE = new List<string>
{
    "Brand new",
    "Never used",
    "Slightly used",
    "Used",
    "Heavily used",
    "Damaged",
    "Ruined",
    "Run for your life"
};

我正在课堂上以这种方式建立一个选择列表:

public string mItemState { get; set; }
public IEnumerable<SelectListItem> mItemStateList { get; set; }

mItemStateList = new SelectList(ValueDomain.LIST_ITEM_STATE);

而且,在我看来,我像这样渲染下拉列表:

Item State: @Html.DropDownListFor(_item => _item.mItemState, Model.mItemStateList, String.Empty)

下拉列表就像魅力一样,但我一直在寻找在Brand New保留String.Emtpty选项时通过默认值设置所选值的方法。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

我认为你必须这样做:

public static readonly List<string> LIST_ITEM_STATE = new List<string>
{
    string.Empty,
    "Brand new",
    "Never used",
    "Slightly used",
    "Used",
    "Heavily used",
    "Damaged",
    "Ruined",
    "Run for your life"
};

并在SelectList

中设置所选值
 mItemStateList = new SelectList(ValueDomain.LIST_ITEM_STATE, "Brand new");

或者确保将属性的值设置为您想要的初始值:

_item.mItemState = "Brand new";

答案 1 :(得分:0)

像这样定义你的SelectList:

mItemStateList = new SelectList(ValueDomain.LIST_ITEM_STATE, mItemState);