@ Html.DropDownListFor未设置默认选定值

时间:2012-12-07 00:28:44

标签: c# asp.net asp.net-mvc asp.net-mvc-4 html-helper

无法弄清楚为何无法在此设置默认值:

@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和string(“2”ou 2)。 无法选择默认值

2 个答案:

答案 0 :(得分:1)

为什么使用相同的对象存储选定的值并显示选项? 尝试使用其他模型字段:

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

答案 1 :(得分: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>