gpt4 book ai didi

c# - 将默认值设置为选择列表

转载 作者:太空宇宙 更新时间:2023-11-03 13:39:28 24 4
gpt4 key购买 nike

我有这个字符串列表:

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 选项。我怎么能那样做?

最佳答案

我认为你必须这样做:

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";

关于c# - 将默认值设置为选择列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17619331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com