解决方法
添加以下内容以查看以创建下拉列表(更改Model.Year以更正模型上的属性)
<div class="editor-field"> @Html.DropDownList("Years",new SelectList(ViewBag.Years as System.Collections.IEnumerable,Model.Year)) @Html.ValidationMessageFor(model => model.Year) </div>
private void GetYears() { List<int> Years = new List<int>(); DateTime startYear = DateTime.Now; while (startYear.Year <= DateTime.Now.AddYears(3).Year) { Years.Add(startYear.Year); startYear = startYear.AddYears(1); } ViewBag.Years = Years; }
GetYears();