我有一个DropDownListFor月号的静态列表,如果我选择的第一个月1,使编辑会发现DropDownListFor重复正月这样 如何使DropDownListFor不使用静态列表重复数据库数据?
DropDownListFor代码:
@Html.DropDownListFor(model=>model.FinancialInfo.FinancialExpiryMonth, (IEnumerable<SelectListItem>)ViewBag.FinancialMonths, (Model.FinancialInfo == null || Model.FinancialInfo.ExpiryDate == null) ? String.Empty : Model.FinancialInfo.ExpiryDate.Value.Month.ToString(), new { @class = "description-text" })
注:FinancialInfo.FinancialExpiryMonth是使其成为View模型的元数据元素。
守则ViewBag.FinancialMonths的:
ViewBag.FinancialMonths = ListOfNumbers(1, 12).Select(m => new SelectListItem { Text = m.ToString(), Value = m.ToString() });`
的ListOfNumbers的代码:
public List<int> ListOfNumbers(int startNum, int endNum)
{
List<int> listOfNumbers = new List<int>();
for (int i = startNum; i <= endNum; i++)
{
listOfNumbers.Add(i);
}
return listOfNumbers;
}
对不起,您的问题是什么? – Richard
'新的SelectList(Enumerable.Range(1,12))'是你所需要的。 –
我喜欢用这个Enumerable.Range但我需要从第一个元素显示数据库中选择的项目,作为然后你只需设置FinancialExpiryMonth'财产'的值以匹配您在GET想选择的选项列表中选择 –