Html.DropDownList default selected item

我的未来我决定 提交于 2019-12-01 16:12:20

问题


I am displaying a list of dropDownLists inside a ForEach loop.

This is the code

@foreach (var task in Model.TaskList)
    {
        <tr>
            <td>@Html.DropDownList("ModuleID", new SelectList(Model.ModuleList, "ModuleID", "Ordre"))
            </td>
        </tr>
    }

I want the Module of each Task to be selected by default. This could easily be done using DropDownList if only I was binding the dropDownList to a view model, but that's not the case.

Any ideas ?


回答1:


This could easily be done using DropDownList if only I was binding the dropDownList to a view model

You already know the correct answer and are still asking the question. And are still not using a view model.

What else can I say, you could pass the value of the element that you want preselected as 4th argument of the SelectList constructor:

@Html.DropDownList(
    "ModuleID", 
    new SelectList(Model.ModuleList, "ModuleID", "Ordre", task.ModuleID)
)


来源:https://stackoverflow.com/questions/16957360/html-dropdownlist-default-selected-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!