Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alphabetically Ordering a SelectList in MVC

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

like image 757
Myzifer Avatar asked Jan 25 '11 16:01

Myzifer


3 Answers

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 --"
) %>
like image 108
Darin Dimitrov Avatar answered Oct 11 '22 16:10

Darin Dimitrov


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");
like image 24
Myzifer Avatar answered Oct 11 '22 14:10

Myzifer


In case where you have an editor template for your dropdownlist, this will sort it based on text. selectList.OrderBy(x => x.Text)

like image 28
h3n Avatar answered Oct 11 '22 15:10

h3n