本文介绍了如何对ASp.net MVC下拉列表进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码

    List<SelectListItem> list = new List<SelectListItem>()
    {
        new SelectListItem() { Text = "bob", Value = "bob"},
        new SelectListItem() { Text = "apple", Value = "apple"},
        new SelectListItem() { Text = "grapes", Value = "grapes"},
    };

这将用于与asp.net mvc html helper绑定.但是,我想在绑定它之前对其进行排序.我该怎么办?

This will be used for binding with the asp.net mvc html helper. However I want to sort it before I bind it. How could i do this?

推荐答案

在这里:

List<SelectListItem> list = new List<SelectListItem>()
{
    new SelectListItem() { Text = "apple", Value = "apple"},
    new SelectListItem() { Text = "bob", Value = "bob"},
    new SelectListItem() { Text = "grapes", Value = "grapes"},
};

排序:)

对不起,无法阻止自己:)

Sorry, couldn't stop myself:)

编辑

看起来好像您需要:

var fruits = new List<string> {"apple", "bob", "grapes"};
fruits.Sort();
var fruitsSelectList = new SelectList(fruits);

然后在视图中

Html.DropDownList("Fruit",fruitsSelectList);

这篇关于如何对ASp.net MVC下拉列表进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 10:10