【问题标题】:Dynamically fill @Html.DropDownList with numbers from 1 - x用 1 - x 的数字动态填充 @Html.DropDownList
【发布时间】:2020-01-29 20:45:24
【问题描述】:

在视图中我有以下内容:

@Html.DropDownList("numberup", new List<SelectListItem>
{
    new SelectListItem{Text = "1", Value = "1"},
    new SelectListItem{Text = "2", Value = "2"}
}, "Select NumberUp", new { id = "numberup", @class = "form-control", @style = "width: auto; margin: 0 0 25px 0;" })

我想动态创建选择列表项,而不是一一输入。我该怎么做?

【问题讨论】:

  • 您有现有的项目清单吗?还是您需要一定范围的数字?请详细说明此列表的预期结果。
  • 就像标题说的我需要填充下拉列表,每个选择列表项递增一。列表文本和值需要为 1-30。我知道有一个可枚举的范围,但我不想使用模型。

标签: asp.net model-view-controller razor-pages


【解决方案1】:

使用Enumerable.Range 构建SelectListItems 的list 并将其分配给您的DropDownList

@{
    var list = Enumerable.Range(1, 30).Select(x => x.ToString()).Select(x => new SelectListItem() { Text = x, Value = x }).ToList();
}

@Html.DropDownList("numberup", list, "Select NumberUp", new { id = "numberup", @class = "form-control", @style = "width: auto; margin: 0 0 25px 0;" })

HTH

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 2015-11-12
    • 2019-04-11
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多