How to sort the ASp.net MVC drop-down list?

I have this code

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

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

+4
source share
8 answers

Here you go:

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

Sorting:)

Sorry, I couldn’t stop myself :)

EDIT

It looks like you need:

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

and then in sight

 Html.DropDownList("Fruit",fruitsSelectList); 
+9
source

If you can use LINQ, then:

 list.OrderBy(x => x.Value) 

or

 list.OrderByDescending(x =>x.Value) 

must do it.

change

This should read:

 list = list.OrderBy(x => x.Value); 
+14
source
 var sorted = (from li in list orderby li.Text select li).ToList(); 

Voila !!

+2
source

you can also sort it on client side using javascript (jquery)

By the way, if you know that the list items just sort them yourself:

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

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;

0
source

Isn't the idea of ​​MVC separate function and mapping? What if you want to reuse the same list with different orders?

I would have thought it would be better since it only sorts if for the specified control.

Add the property to the model you use for the view:

 public SelectList Fruit { get; set; } 

Fill this list in your constructor (I use Entity Framework):

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

Then add your selection list:

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

A very simple way to handle this in the controller:

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

------- Save Procedure ----- (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; } } 
-2
source

All Articles