2013-01-22 43 views
4

我有这个代码的工作和平,它显示在我的MVC3 Razor Web应用程序中填充DropDownList。如何禁用MVC3 Razor中的DropDownListFor?

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

我需要保持DropDownList填充但DISABLED它,所以用户无法在列表中选择一个值。

你能指出我在正确的方向吗?请给我一个代码样本。谢谢!

回答

14

像这样,通过指定HTML属性。 DropDownListFor

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

谢谢(Model.RemindersList,“Item1”,“Item2”),new {disabled =“disabled”}) – GibboK

+1

请注意,禁用的表单字段不会提交给服务器。 –

1

我的解决方案由于拉维打

您的砸在我的特定情况下工作呢代码为@ Html.DropDownListFor(X => x.ReminderTime, 新的SelectList
@Html.DropDownListFor(x => x.ReminderTime, 
       new SelectList(Model.RemindersList, "Item1 ", "Item2"), new { disabled = "disabled" }) 
0
<select disabled> 
@Html.DropDownListFor(x => x.ReminderTime, 
     new SelectList(Model.RemindersList, "Item1 ", "Item2")) 
</select>