我尝试进入下拉列表的当前日期我尝试了很多变种如何实现这一点..所以这是我的示例代码..
@{ var days = Enumerable.Range(1, 31).
Select(i => new SelectListItem {Value = i.ToString(), Text = i.ToString(), Selected = i == DateTime.Now.Day}).ToList();
var month = Enumerable.Range(1, 12).
Select(i => new SelectListItem {Value = i.ToString(), Text = System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.GetMonthName(i), Selected = i == DateTime.Now.Month}).ToList();
var years = Enumerable.Range(2000, DateTime.Now.Year - 2000 + 4).
Select(i => new SelectListItem {Value = i.ToString(), Text = i.ToString(), Selected = i == DateTime.Now.Year}).ToList();
}
@Html.DropDownListFor(model => model.SDay, days)
@Html.DropDownListFor(model => model.SMonth, month)
@Html.DropDownListFor(model => model.SYear, years)
你可以看到我在日期,月份和年份的选定日期中设置了今天,调试器显示我今天被选中Debugger但是在查看日设置为1