对于未来的用户(我知道这很旧,但我自己在寻找解决方案时遇到了它),如果您有一个具有单个选定值的现有 SelectList,那么这个助手将为您提供 Add 和 Prepend 方法:-
/// <summary>
/// A static helper class providing extension methods to SelectList instances
/// </summary>
public static class SelectListExtensions
{
/// <summary>
/// Adds the specified value to the select list.
/// </summary>
/// <param name="selectList">The select list.</param>
/// <param name="value">The value.</param>
/// <param name="text">The text.</param>
/// <param name="selected">if set to <c>true</c> [selected].</param>
/// <returns>The modified/rebuilt select list</returns>
public static SelectList Add(this SelectList selectList, string value, string text, bool selected = false)
{
var itemList = selectList.ToList().FluentAdd(
new SelectListItem { Value = value, Text = text, Selected = selected }
);
return new SelectList(itemList, "Value", "Text");
}
/// <summary>
/// Prepends the specified value to the select list.
/// </summary>
/// <param name="selectList">The select list.</param>
/// <param name="value">The value.</param>
/// <param name="text">The text.</param>
/// <param name="selected">if set to <c>true</c> [selected].</param>
/// <returns>The modified/rebuilt select list</returns>
public static SelectList Prepend(this SelectList selectList, string value, string text, bool selected = false)
{
var itemList = selectList.ToList();
itemList.Insert(0, new SelectListItem { Value = value, Text = text, Selected = selected });
return new SelectList(itemList, "Value", "Text", selectList.SelectedValue);
}
}