如何对ASp.net MVC下拉列表进行排序?

时间:2009-11-08 23:12:19

标签: c# 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帮助器绑定。但是我想在绑定它之前对它进行排序。我怎么能这样做?

8 个答案:

答案 0 :(得分:16)

如果你可以使用LINQ:

list.OrderBy(x => x.Value)

list.OrderByDescending(x =>x.Value)

应该这样做。

修改

应该阅读;

list = list.OrderBy(x => x.Value);

答案 1 :(得分:10)

你走了:

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

排序:)

抱歉,无法阻止自己:)

修改

看起来好像你需要:

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

然后在视图中

Html.DropDownList("Fruit",fruitsSelectList);

答案 2 :(得分:2)

var sorted = (from li in list
             orderby li.Text
             select li).ToList();

瞧!! ​​

答案 3 :(得分:0)

您也可以使用javascript(jquery)

在客户端对其进行排序

顺便说一句,如果您知道列表中的元素,请自行排序:

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

答案 4 :(得分:0)

MVC的想法是不是将功能和显示分开?如果您想重复使用不同排序的相同列表,该怎么办?

我认为这是最好的,因为它只对指定的控件进行排序。

将属性添加到您用于视图的模型中:

public SelectList Fruit { get; set; }

在构造函数中填充该列表(我正在使用实体框架):

model.Fruit= new SelectList(db.tblFruit.Select(f => new { Id = f.ID, Name = f.Name }), "ID", "Name", "[Select Fruit]");

然后添加您的选择列表:

@Html.DropDownListFor(x => x.ID, new SelectList(Model.Fruit.OrderBy(y => y.Text), "Value", "Text"), "-- Select One --", new { @class = "form-control" })

答案 5 :(得分:0)

在Controller中处理它的一种非常简单的方法:

ViewBag.change_week = new SelectList(db.weeks.OrderBy(x=> x.week_guid), "week_guid", "week_number");

答案 6 :(得分:-1)

list.Sort

List<SelectListItem> list = new List<SelectListItem>() 

{new SelectListItem(){Text =“bob”,Value =“bob”},
             new SelectListItem(){Text =“apple”,Value =“apple”},
             new SelectListItem(){Text =“grapes”,Value =“grapes”},};

list.sort;

答案 7 :(得分:-2)

-------存储过程-----(SQL)

USE [Your Database]
GO

CRATE PROC [dbo].[GetAllDataByID]
@ID int


AS
BEGIN
        SELECT * FROM Your_Table
        WHERE ID=@ID
        ORDER BY Your_ColumnName 
END

---------- --------- Default.aspx的

 <asp:DropDownList ID="ddlYourTable" runat="server"></asp:DropDownList>

--------- Default.aspx.cs -------

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<YourTable> table= new List<YourTable>();
                YourtableRepository tableRepo = new YourtableRepository();



                int conuntryInfoID=1;
                table= tableRepo.GetAllDataByID(ID);

                ddlYourTable.DataSource = stateInfo;
                ddlYourTable.DataTextField = "Your_ColumnName";
                ddlYourTable.DataValueField = "ID";
                ddlYourTable.DataBind();



            }
        }

------- LINQ Helper Class ----

public class TableRepository
    {
        string connstr;

        public TableRepository() 
        {
            connstr = Settings.Default.YourTableConnectionString.ToString();
        }

        public List<YourTable> GetAllDataByID(int ID)
        {
            List<YourTable> table= new List<YourTable>();
            using (YourTableDBDataContext dc = new YourTableDBDataContext ())
            {
                table= dc.GetAllDataByID(CID).ToList();
            }
            return table;
        }
    }