Asp.net mvc 如何将项添加到具有可枚举范围列表项的DropDownList?
我有一个带有静态选择列表的Asp.net mvc 如何将项添加到具有可枚举范围列表项的DropDownList?,asp.net-mvc,razor,Asp.net Mvc,Razor,我有一个带有静态选择列表的DropDownListFor 代码: 我需要的第一项是wordMonth如何使用此静态DropDownListFor 我不确定您在下拉列表中签入什么内容,但我建议您这样做: @Html.DropDownListFor(model => model.FinancialInfo.FinancialExpiryMonth, //Genaration of month names Enumerable.Range(1, 12).Select(x=> ne
DropDownListFor
代码:
我需要的第一项是wordMonth
如何使用此静态DropDownListFor
我不确定您在
下拉列表中签入什么内容,但我建议您这样做:
@Html.DropDownListFor(model => model.FinancialInfo.FinancialExpiryMonth,
//Genaration of month names
Enumerable.Range(1, 12).Select(x=> new SelectListItem
{
Value = x.ToString(),
Text = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x)
}),
"Month", //First Value if nothing selected
new { @class = "description-text" })
最后,当项目等于模型值时,只需添加所选=true即可解决此问题
@Html.DropDownListFor(model => model.FinancialInfo.FinancialExpiryMonth, Enumerable.Range(1, 12).Select(x => new SelectListItem
{
Value = x.ToString(),
Text = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x),
}), "Month", new { @class = "description-text" })
将第三个参数替换为“Month”
@Stephen Muecke,它将是Month,但在编辑操作中,所选项目是一个值,而不是“Month”
需要选择所选的编辑。我在您的。您需要在GET方法中设置属性FinancialExpiryMonth
的值,以匹配其中一个选项的值(并将模型传递给视图)。此处的月份是一个位置holder@AhmedHossamAlhansy你能详细说明一下吗?我需要这个列表有一个名为“月”的字符串
列表中的所选项目是Model.FinancialInfo.ExpiryDate.value.Month.ToString()的值。
需要从列表中选择与Model.FinancialInfo.ExpiryDate.value.Month.ToString()相等的所选项目。
这是错误的。它是您绑定到的属性的值,用于确定所选内容。如果属性值为FinancialExpiryMonth
为3
,则将选择“三月”
。如果FinancialExpiryMonth
的值不在1和12之间,将选择第一个选项。绑定到属性时,将忽略设置SelectListItem
的Selected
属性。但是他不能在selectList之外添加其他值这与我的评论有什么关系?删除Selected=(Model.FinancialInfo….
位,您将看到完全相同的结果!但必须在第一页行@{…}
中添加此条件才能进行此验证
@Html.DropDownListFor(model => model.FinancialInfo.FinancialExpiryMonth, Enumerable.Range(1, 12).Select(x => new SelectListItem
{
Value = x.ToString(),
Text = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x),
}), "Month", new { @class = "description-text" })