2

我有这个字符串列表:

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选项的同时通过默认设置选定值的方法。我怎么能那样做?

4

2 回答 2

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";
于 2013-07-12T16:17:35.100 回答
0

像这样定义您的 SelectList:

mItemStateList = new SelectList(ValueDomain.LIST_ITEM_STATE, mItemState);
于 2013-07-12T16:20:02.447 回答