Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将默认值设置为选择列表_C#_Asp.net Mvc_Html.dropdownlistfor_Selectlist - Fatal编程技术网

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

C# 将默认值设置为选择列表,c#,asp.net-mvc,html.dropdownlistfor,selectlist,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" }; dropdownl

我有以下字符串列表:

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

dropdownlist工作起来很有魅力,但我一直在寻找方法,在保留
String.Emtpty
选项的同时,通过defaul在
Brand New
设置所选值。我该怎么做呢?

我想你必须这样做:

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"
};
或者确保属性的值设置为所需的初始值:

_item.mItemState = "Brand new";

按如下方式定义您的选择列表:

Item State: @Html.DropDownListFor(_item => _item.mItemState, Model.mItemStateList, String.Empty)
mItemStateList = new SelectList(ValueDomain.LIST_ITEM_STATE, mItemState);

请不要在属性前面加
m
。它不仅是一个可怕的匈牙利符号,而且完全没有必要,因为属性当然是成员。那么你有什么建议?这些都是我们公司实施的实践,可以轻松区分成员、方法参数和内部方法变量。如果您的公司坚持在固有成员的前面加上前缀,而不能是其他任何内容,那么我建议您解雇编写标准的人。:)
_item.mItemState = "Brand new";
mItemStateList = new SelectList(ValueDomain.LIST_ITEM_STATE, mItemState);