如何使RaDor中的DropDownListFor无法选择

时间:2012-10-10 11:58:06

标签: asp.net-mvc asp.net-mvc-3 razor

在我的Razor视图中,我使用DropDown List我想让这个控件不可选。怎么做,(请发一个样品)?

 <div class="editor-field">
        @Html.DropDownListFor(x => x.CandidateId, Model.CandidatesList)
    </div>

4 个答案:

答案 0 :(得分:9)

您使用disabled属性,其值为true和false。

<div class="editor-field">
    @Html.DropDownListFor(x => x.CandidateId, Model.CandidatesList, new { disabled = "true" })
</div>

答案 1 :(得分:2)

您可能希望查看DropDownListFor的覆盖,它会获取HTML属性的字典,并可能传递类似'disabled =“true”'的内容。

http://msdn.microsoft.com/en-us/library/ee703436(v=vs.108).aspx

    <div class="editor-field">
        @Html.DropDownListFor(x => x.CandidateId, Model.CandidatesList, new { disabled = "true" })
    </div>

答案 2 :(得分:1)

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

答案 3 :(得分:0)

你可以试试这个:

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

但是在提交后你无法获得控制器中的下拉值。您可以使用隐藏字段设置下拉值。