2

Can´t figure out why can´t set a default value here to be displayed:

@Html.DropDownListFor(m => m.storeLocations, new SelectList(Model.storeLocations, "id",
"caminhoRepositorio", Model.idArmazenamento), new { @class = "largeField" })

The dropdown had the folowing configuration:

 <select class="largeField" id="storeLocations" name="storeLocations">
      <option value="2">c:/item1</option>
      <option value="3">d:/item2</option>
</select>

as a object "Model.idArmazenamento" I´ve already tried with int and string ("2" ou 2). Can´t get a default value selected

Guilherme Longo
  • 57
  • 3
  • 10

2 Answers2

1

Why are you using the same object to store the selected value and to show the options? Try to use a different model field:

@Html.DropDownListFor(m => m.SelectedStoreLocations, new SelectList(Model.storeLocations, "id",
"caminhoRepositorio", Model.idArmazenamento), new { @class = "largeField" })
MuriloKunze
  • 15,195
  • 17
  • 54
  • 82
0

Managed to work in a very old style:

<td>
    <select class="largeField" id="storeLocations" name="storeLocations">                         
        @foreach (var location in Model.storeLocations)
        {
            if (location.id == Model.idArmazenamento) { 
                <option value="@location.id" selected="@location.id">@location.caminhoRepositorio</option>
            } else {
                <option value="@location.id">@location.caminhoRepositorio</option>
            }
        }
    </select>
</td>
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Guilherme Longo
  • 57
  • 3
  • 10