好的,做到了!供将来参考这里是解决方案:
我为 DropDownListFor 创建了一个扩展方法:
public static MvcHtmlString KeywordDropDownListFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression,
IEnumerable<SelectListItem> selectList, object htmlAttributes)
{
Func<TModel, TValue> method = expression.Compile();
string value = method(helper.ViewData.Model) as string;
if (String.IsNullOrEmpty(value))
{
List<SelectListItem> newItems = new List<SelectListItem>();
newItems.Add(new SelectListItem
{
Selected = true,
Text = Strings.ChooseAKeyword,
Value = String.Empty
});
foreach (SelectListItem item in selectList)
{
newItems.Add(item);
}
return helper.DropDownListFor(expression, newItems, htmlAttributes);
}
return helper.DropDownListFor(expression, selectList, htmlAttributes);
}