【发布时间】:2018-08-31 23:45:47
【问题描述】:
我有月份'DropDownListFor,我想选择当前月份作为默认月份我尝试了这两个选项
@{
var currentMonth = month.FirstOrDefault(x => x.Id == DateTime.Now.Month).Id;
}
1.
@Html.DropDownListFor(x => x.monthId, new SelectList(month, "Id", "Name", currentMonth ))
2.
@Html.DropDownListFor(x => x.monthId, month.Select(x => new SelectListItem
{ Text = x.Name.ToString(), Value = x.Id.ToString(), Selected = (x.Id == currentMonth ?true:false)})),
但两者都不起作用。 我怎样才能实现我的目标?
【问题讨论】:
-
为什么这个非常基本的问题被投了三遍?
-
向我们展示您的模型?至少是使用视图的控制器操作
-
month是什么,你有没有调试你的代码,currentMonth变量不为空? -
month 是一个 List
,其中 Id 为 Value,Text 为 Name
标签: c# asp.net asp.net-mvc razor html.dropdownlistfor