我使用Edtor模板作为DateTime属性(生日),显示三个下拉列表以显示年,月和日。呈现下拉列表时,它们的name属性分别是Birthday.Year,Birthday.Month和Birthday.Day。但是,当您发布表单时,它们不会绑定到Birthday属性。
DateTime.cshtml
@model DateTime
List<SelectListItem> days = new List<SelectListItem>();
for (int i = 1; i <= 31; i++)
{
days.Add(new SelectListItem() { Text = i.ToString(), Value = i.ToString(), Selected = (i == Model.Day ? true : false) });
}
//Same for Month and Year with some modification
@Html.DropDownListFor(m => m.Day, days, "<Year>")
@Html.DropDownListFor(m => m.Month, months, "<Month>")
@Html.DropDownListFor(m => m.Year, years, "<Day>")