在 MVC 中按字母顺序对 SelectList 进行排序

发布于 2024-10-14 05:10:26 字数 413 浏览 2 评论 0原文

类似示例

问题是我的选择列表可能有数据(加上它的格式类似于[福特IV 200 xyx]),我想在其中退休(通过仅显示在第三列中具有一点值 true 的记录,这也是我需要弄清楚如何做的事情),大问题是如果用户添加比如说另一辆福特汽车,它现在会一直显示在下拉列表的底部,它看起来会非常混乱,甚至可能被忽略,所以有什么想法吗?

ps 将 jquery 添加到标签中,以防这是一个可能的解决方案,因为我可以在这个项目中使用它。

编辑 - 对于第三列位值过滤器,这是解决方案

similar example

Problem is my selectlist might have data(plus it's format is something like [Ford IV 200 xyx]) in it which I want to retire(by only displaying records which has a bit value of true in it's 3rd column which is also something I need to figure out how to do), big problem is if the user adds in say another Ford which would now display all the way at the bottom of the drop down it would look very messy and could even be overlooked so any ideas?

p.s. added jquery to the tags in case that was a possible solution to this since I am able to use that in this project.

Edit - For that 3rd column bit value filter here's the solution

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷途知返 2024-10-21 05:10:26

您可以使用 OrderBy 扩展方法:

<%: Html.DropDownListFor(
    x => x.ModelId, 
    new SelectList(Model.VehicleModels.OrderBy(x => x.Name), "Id", "Name"), 
    "-- Select a model --"
) %>

You could use the OrderBy extension method:

<%: Html.DropDownListFor(
    x => x.ModelId, 
    new SelectList(Model.VehicleModels.OrderBy(x => x.Name), "Id", "Name"), 
    "-- Select a model --"
) %>
野却迷人 2024-10-21 05:10:26

感谢达林,我能够想出他的稍微修改过的解决方案,这反而导致我在虚拟机中解决这个问题,如下所示

List<Reason> reasonList = _db.Reasons.OrderBy(m=>m.Description).ToList();
        ReasonList = new SelectList(reasonList, "Id", "Description");

thanks to Darin I was able to come up with the slightly modified solution of his which instead lead to me resolving this in the VM like so

List<Reason> reasonList = _db.Reasons.OrderBy(m=>m.Description).ToList();
        ReasonList = new SelectList(reasonList, "Id", "Description");
城歌 2024-10-21 05:10:26

如果您的下拉列表有编辑器模板,这将根据文本对其进行排序。
<代码>
selectList.OrderBy(x => x.Text)

In case where you have an editor template for your dropdownlist, this will sort it based on text.

selectList.OrderBy(x => x.Text)

提笔书几行 2024-10-21 05:10:26
List<string> TestList = new List<string>();

foreach(//SomeCode)
{

TestList.Add(//Item);

}

TestList = TestList.OrderBy(n => n.TestItem).ToList();
List<string> TestList = new List<string>();

foreach(//SomeCode)
{

TestList.Add(//Item);

}

TestList = TestList.OrderBy(n => n.TestItem).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文