2

无法弄清楚为什么不能在此处设置要显示的默认值:

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

下拉列表具有以下配置:

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

作为对象“Model.idArmazenamento”,我已经尝试过使用 int 和字符串(“2”ou 2)。无法选择默认值

4

2 回答 2

1

为什么要使用同一个对象来存储选定的值并显示选项?尝试使用不同的模型字段:

@Html.DropDownListFor(m => m.SelectedStoreLocations, new SelectList(Model.storeLocations, "id",
"caminhoRepositorio", Model.idArmazenamento), new { @class = "largeField" })
于 2012-12-07T00:57:54.377 回答
0

设法以一种非常古老的方式工作:

<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>
于 2012-12-07T00:58:07.927 回答