Click here to Skip to main content
14,878,090 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a requirement like i want to display all the country names into dropdown from sql database table except canada , i am able to display all the country names but not able to display all the country names except canada can any one help me out, i am using below code for displaying all the country names in dropdown list

@Html.DropDownListFor(model => model.nationality,
(SelectList)@ViewBag.NationalityDropDownValues, "---Select Nationality ---", new { @class = "m-wrap " })

Thanks in advance

1 solution

What do you have against Canada?

When you populate ViewBag.NationalityDropDownValues only include the non-Canadian countries. You haven't posted the code where you populate that property so we can't give you specific help but it'll probably be something like

ViewBag.NationalityDropDownValues = countries.Where(c => C.Name != "Canada");


ViewBag.NationalityDropDownValues = countries.Where(c => C.ID != 123);

where "123" is the ID for Canada in your country collection. If you use ViewBag.NationalityDropDownValues in places that do need to show Canada then it's best to pass your view two collections, one with Canada in it and one filtered.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900