Html.DropDownList默认选定的项目 [英] Html.DropDownList default selected item

查看:200
本文介绍了Html.DropDownList默认选定的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在ForEach循环中显示一个dropDownLists列表.

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

这是代码

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

我希望默认选择每个TaskModule.如果仅将dropDownList绑定到视图模型,则可以使用DropDownList轻松完成此操作,

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.

有什么想法吗?

推荐答案

如果只有我绑定了 dropDownList到视图模型

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.

我还能说什么,您可以将要预先选择的元素的值作为SelectList构造函数的第4个参数传递:

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)
)

这篇关于Html.DropDownList默认选定的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆