Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC4: How to sort this selectlist/dropdownlist

I'm working with MVC4.

I have this following dropdownlist/selectlist:

@Html.DropDownList("EmployerID", String.Empty)

ViewBag.EmployerID = new SelectList(db.Employers, "EmployerID", "Name", contact.EmployerID);

and I want this dropdownlist/selectlist to be sorted by the "Name". What should I do?

Many thanks! :)

like image 887
user2423801 Avatar asked Dec 26 '22 03:12

user2423801


1 Answers

Add OrderBy to your db.Employers

@Html.DropDownList("EmployerID", String.Empty)

ViewBag.EmployerID = new SelectList(db.Employers.OrderBy(x => x.Name), 
    "EmployerID", "Name", contact.EmployerID);
like image 102
AliRıza Adıyahşi Avatar answered Jan 08 '23 16:01

AliRıza Adıyahşi