Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.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
Asp.net mvc 如何将项添加到具有可枚举范围列表项的DropDownList?_Asp.net Mvc_Razor - Fatal编程技术网

Asp.net mvc 如何将项添加到具有可枚举范围列表项的DropDownList?

Asp.net mvc 如何将项添加到具有可枚举范围列表项的DropDownList?,asp.net-mvc,razor,Asp.net Mvc,Razor,我有一个带有静态选择列表的DropDownListFor 代码: 我需要的第一项是wordMonth如何使用此静态DropDownListFor 我不确定您在下拉列表中签入什么内容,但我建议您这样做: @Html.DropDownListFor(model => model.FinancialInfo.FinancialExpiryMonth, //Genaration of month names Enumerable.Range(1, 12).Select(x=> ne

我有一个带有静态选择列表的
DropDownListFor
代码:

我需要的第一项是word
Month
如何使用此静态
DropDownListFor


我不确定您在
下拉列表中签入什么内容,但我建议您这样做:

@Html.DropDownListFor(model => model.FinancialInfo.FinancialExpiryMonth,
    //Genaration of month names
 Enumerable.Range(1, 12).Select(x=> new SelectListItem
 {
    Value = x.ToString(),
    Text = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x)
 }), 
 "Month", //First Value if nothing selected
 new { @class = "description-text" })

最后,当项目等于
模型值时,只需添加所选=true即可解决此问题

                    @Html.DropDownListFor(model => model.FinancialInfo.FinancialExpiryMonth, Enumerable.Range(1, 12).Select(x => new SelectListItem
           {
               Value = x.ToString(),
               Text = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x),
           }), "Month", new { @class = "description-text" })

将第三个参数替换为
“Month”
@Stephen Muecke,它将是Month,但在编辑操作中,所选项目是一个值,而不是
“Month”
需要选择所选的编辑。我在您的。您需要在GET方法中设置属性
FinancialExpiryMonth
的值,以匹配其中一个选项的值(并将模型传递给视图)。此处的月份是一个位置holder@AhmedHossamAlhansy你能详细说明一下吗?我需要这个列表有一个名为“月”的字符串
列表中的所选项目是
Model.FinancialInfo.ExpiryDate.value.Month.ToString()的值。
需要从列表中选择与
Model.FinancialInfo.ExpiryDate.value.Month.ToString()相等的所选项目。
这是错误的。它是您绑定到的属性的值,用于确定所选内容。如果属性值为
FinancialExpiryMonth
3
,则将选择
“三月”
。如果
FinancialExpiryMonth
的值不在1和12之间,将选择第一个选项。绑定到属性时,将忽略设置
SelectListItem
Selected
属性。但是他不能在selectList之外添加其他值这与我的评论有什么关系?删除
Selected=(Model.FinancialInfo….
位,您将看到完全相同的结果!但必须在第一页行
@{…}
中添加此条件才能进行此验证
                    @Html.DropDownListFor(model => model.FinancialInfo.FinancialExpiryMonth, Enumerable.Range(1, 12).Select(x => new SelectListItem
           {
               Value = x.ToString(),
               Text = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x),
           }), "Month", new { @class = "description-text" })