AmateurDev AmateurDev -4 years ago 104
ASP.NET (C#) Question

ASP.NET MVC: DropDownList grouped by name not id

I have a dropdown list for locations which is currently grouped by country in an ASP MVC program.

The locations' drop down list is populated using the following line in my ViewModel:

ViewBag.Locations = new MultiSelectList(db.tblLocations, "LocationID", "LocationName", "CountryID");


Currently, the drop down list displays the countries as their IDs rather than by name.

e.g.

1

Location

Location

Location

2

Location

Location


Is there a way to have the countries display in the drop down by name instead of ID?

Any help would be appreciated.

Thanks.

EDIT:

tblLocations:

LocationID

LocationName

CountryID

tblCountries

CountryID

CountryName

Answer Source

you can do it like this :

ViewBag.Locations = new MultiSelectList(db.tblLocations, "LocationID", "LocationName", "tblCountries.CountryName");

but you need to include the tblCountries in tblLocations

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download