在您的示例中,您使用
DropDownList(this HtmlHelper htmlHelper, string name, string optionLabel)
要获得你想要的效果,你应该使用下一个代码:
@Html.DropDownListFor(model => model.FinancialYearID, (SelectList)ViewBag.FinancialYearID, "Select FY", new { @class = "foo" })
(重载为DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel>, Expression<Func<TModel,TProperty>>, IEnumerable<SelectListItem>,
string,
object))
或者,如果您的 SelectList 中的一个标记为选中,请使用下一个代码:
@Html.DropDownListFor(model => model.FinancialYearID, (SelectList)ViewBag.FinancialYearID, new { @class = "foo" })
(重载为DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel>, Expression<Func<TModel,TProperty>>, IEnumerable<SelectListItem>,
object))
附:
不要忘记将 ViewBag 项转换为 SelectList,因为编译器会认为第二个参数是对象。