gpt4 book ai didi

vb.net - HTML.DropDownListFor - SelectList 的语法糖

转载 作者:行者123 更新时间:2023-12-01 14:38:05 28 4
gpt4 key购买 nike

我正在使用 Html.DropDownListFor 来构建一个选择列表。它是一个从 1 到 100 的简单数字列表。参数之一 - selectList As System.Collections.Generic.IEnumerable(Of SelectListItem) - 是列表的选项,我通常手动构建它,像这样:

@Html.DropDownListFor(Function(x) x.Sorting.IsAscending, _ 
New SelectList(New Dictionary(Of String, Boolean) From _
{{"Sort Ascending", True}, {"Sort Descending", False}}, "value", "key"))

或者从一个枚举中,像这样:

@Html.DropDownListFor(Function(x) x.Sorting.SortFieldCurrent, _
New SelectList(Model.Sorting.SortFields, "value", "key"))

但这次我想要一个从 1 到 100 的列表。我拒绝手动创建它:)

是否有一些甜蜜的 LINQ 魔法可以为我构建列表?

最佳答案

使用Enumerable.Range生成数字范围:

C#:

IEnumerable<int> range = Enumerable.Range(1, 100);

VB(由 http://www.developerfusion.com/tools/convert/csharp-to-vb/ 提供):

Dim range As IEnumerable(Of Integer) = Enumerable.Range(1, 100)

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.range.aspx

关于vb.net - HTML.DropDownListFor - SelectList 的语法糖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9040602/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com