2

我需要在我的选择列表中添加一个选项,例如:

<option value="">Select an Option Below</option>

这是我生成选择列表的方法:

不确定在哪里添加此选项。谢谢

public static IEnumerable<SelectListItem> GetBuildingClubs(List<BuildingClub> localClubs, List<BuildingClub> psfyClubs)
{
    var buildingClubList = new List<SelectListItem>();
    IEnumerable<BuildingClub> allBuildingClubs = localClubs.Union(psfyClubs);
    foreach (BuildingClub b in allBuildingClubs)
    {
        buildingClubList.Add(new SelectListItem
        {
            Value = b.MasterCustomerId,
            Text = b.NewBuildingClubName

        });
    }

    buildingClubList.OrderBy(c => c.Text);
    //Prepend the default option here???
    return buildingClubList;
}
4

6 回答 6

7

无需在集合中添加默认值,您可以直接在视图中执行此操作:

@Html.DropDownListFor(model => model.SelectedValue, Model.BuildingClubList, "Select an Option Below")

带有"Select an Option Below"空白值的项目将出现在您的下拉列表中。

假设您的模型包含一个由您的方法BuildingClubList初始化的属性。GetBuildingClubs

于 2013-10-24T15:09:11.920 回答
3

您可以使用Insert类似的方法

 buildingClubList.Insert(0, new SelectListItem
    {
        Value = "",
        Text = "Select an Option Below"

    });

或者

SelectListItem在循环之前添加默认值

于 2013-10-24T15:04:50.837 回答
1

对于未来的用户(我知道这很旧,但我自己在寻找解决方案时遇到了它),如果您有一个具有单个选定值的现有 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);
    }
}
于 2018-01-26T15:33:04.980 回答
0

只需添加一个

buildingClubList.Add(new SelectListItem
{
    Value = "",
    Text = "Select an Option Below"
});

在你的 foreach 循环之前。

于 2013-10-24T15:03:49.877 回答
0

在进入你的 foreach 循环之前,添加:

buildingClubList.Add(new SelectListItem
        {
            Value = "",
            Text = "Select an Option Below"

        });

在他们选择某些东西后,您当然要验证它不是这个选项

于 2013-10-24T15:03:56.300 回答
0

这是旧的,如果您想要“提示”选项,@RédaMattar 的答案是最好/正确的答案,但我偶然发现它希望手动向 SelectList 添加一个有价值的选项(例如 - 一个允许客户端触发“新" 行动) 这似乎是最干净的方式:

 selectList = selectList.Prepend(
                 new SelectListItem { Value = "New", Text = "New", Selected = false }
              );
于 2019-12-10T02:14:56.973 回答