0

When I have an object with a value set, it is only shown initially if I leave out the "--Select--" parameter.

Any idea why that would be?

@Html.DropDownListFor(m => m.FlightDetails.AirlineId, ViewBag.AirlineList as SelectList, "--Select--", new { @class = "form-control selectpicker", @data_live_search = "true", @id = flight.Id + "AirlineId" })
Izzy
  • 400
  • 3
  • 9

2 Answers2

0

Server-side:

You should set a default value for third params, look like this (Pay attention to SelectedDefaultValueHere)

var ViewBag.AirlineList = new SelectList(`listOfItemHere`,
    "ValueOfDropdownItem", 
    "TextToDisplay",  
    SelectedDefaultValueHere);

Or You can set Selected of SelectListItem like this

new SelectListItem()
{
   Value = "anyValue",
   Text = "AnyText",
   Selected = true
};
Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
0

I found that this was my issue: https://stackoverflow.com/a/52431696/6591937.

I can't set the selected value when creating the SelectList because I'm using the same SelectList in more that one place and they all can have different values.

The way I resolved it in my case was that I passed in "--Select--" and I set the value via javascript (as I'm accessing it only via javascript anyway).

Izzy
  • 400
  • 3
  • 9