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 val开发者_高级运维ue filter here's the solution
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 --"
) %>
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");
In case where you have an editor template for your dropdownlist, this will sort it based on text.
selectList.OrderBy(x => x.Text)
List<string> TestList = new List<string>();
foreach(//SomeCode)
{
TestList.Add(//Item);
}
TestList = TestList.OrderBy(n => n.TestItem).ToList();
精彩评论