如何在MVC3 Razor中禁用DropDownListFor?

8
我有一段可用的代码,用于在我的MVC3 Razor Web应用程序中显示下拉列表。
       @Html.DropDownListFor(x => x.ReminderTime,
            new SelectList(Model.RemindersList, "Item1 ", "Item2"))

我需要保持DropDownList中的选项,但将其禁用,这样用户就无法在列表中选择值。

你能指点我正确的方向吗?请提供一段代码示例。谢谢!

2个回答

19

您可以通过指定HTML属性来实现这样的效果。 DropDownListFor


@Html.DropDownListFor(
    x => x.ReminderTime,
    Model.RemindersList,
    new { disabled = "disabled" }
)

感谢您提供的代码,在我的具体情况下,工作正常的代码是: @Html.DropDownListFor(x => x.ReminderTime, new SelectList(Model.RemindersList, "Item1", "Item2"), new { disabled = "disabled" }) - GibboK
1
请注意,禁用的表单字段将不会提交到服务器。 - Darren Griffith

1

我的解决方案感谢Ravi hit

    @Html.DropDownListFor(x => x.ReminderTime,
                 new SelectList(Model.RemindersList, "Item1 ", "Item2"), new { disabled = "disabled" })

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接