如何将项添加到具有Enumerable.Range列表项的DropDownlistFor?

时间:2016-11-14 12:28:05

标签: asp.net-mvc razor

我有一个带有静态selectList的DropDownListFor 代码:

 @Html.DropDownListFor(model => model.FinancialInfo.FinancialExpiryMonth,
  new SelectList(Enumerable.Range(1, 12)), 
                (Model.FinancialInfo == null 
                  || Model.FinancialInfo.ExpiryDate == null) 
                  ? String.Empty 
                  : null, 
  new { @class = "description-text" })

我需要第一项是单词Month我怎么能用这个静态DropDownListFor做到这一点?

This is example for what i need to do!

2 个答案:

答案 0 :(得分:0)

我不确定你在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" })

答案 1 :(得分:-1)

最后解决了当项目等于Model Value

时添加selected = 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" })