PixelPaul
PixelPaul

Reputation: 2799

asp.net MVC set default value for dropdown list

I'm displaying a dropdown list for countries, which is populated from a database call in the controller. On page load I would like the selected value to default to 'United States'. How do I do this?

Code from view:

<div class="form-group">
            @Html.LabelFor(m => m.Country)
            @Html.DropDownListFor(m => m.Country, new SelectList(Model.CountriesDDL, "CountryCode", "Country"), "--Select--", new { @class = "form-control" })
</div>

Upvotes: 1

Views: 1659

Answers (1)

Shyju
Shyju

Reputation: 218942

In your GET action, you can set the value of Country property to the CountryCode of United states (or whatever country you want to set as default) of your view model

public ActionResult Show()
{
  var vm = new YourViewModel();
  vm.CountriesDDL = GetCountriesFromSomeWhere();
  vm.Country="United States";
  return View(vm);
}

Assuming Country is of type string

Upvotes: 1

Related Questions