Rituraj Mishra
Rituraj Mishra

Reputation: 141

How to set United States as default value of a drop down list of countries

I am using MvC an for getting a dropdownlist of countries I am using the following code can somebody tell me how to set United states as the default value.

This code is in a class called Commons.cs which contains the code for all my dropdownlist used anywhere in the project.

Commons.cs

 public static  IEnumerable<Country> GetCountries()
        {

          return CultureInfo.GetCultures(CultureTypes.SpecificCultures)

                 .Select(x => new Country
                 {
                     ID = new RegionInfo(x.LCID).Name,
                     Name = new RegionInfo(x.LCID).EnglishName
                 })
                                 .GroupBy(c => c.ID)
                                 .Select(c => c.First())
                                 .OrderBy(x => x.Name);

        }

view code

 @Html.DropDownListFor(m => m.Lab.Country, new SelectList(Commons.GetCountries(), "Id", "Name"), new { @style = "width:200px;color:black" })

Upvotes: 0

Views: 539

Answers (2)

Henk Mollema
Henk Mollema

Reputation: 46501

You could initialize it in your controller action. The code would look like this:

var model = new SomeViewModel();
model.Lab = new LabViewModel();
model.Lab.Country = "US"; // Default: United States.

Now the "United States" option should be selected in the drop down by default.

Upvotes: 1

Rituraj Mishra
Rituraj Mishra

Reputation: 141

Ok. I have done it using jQuery:

$('select').val('US');

Upvotes: 0

Related Questions