GibboK
GibboK

Reputation: 73908

How to make a DropDownListFor not selectable in Razor

In my View in Razor I use a DroDownList I would like to have this control not Selectable. How to do it, (please post a sample)?

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

Upvotes: 2

Views: 4317

Answers (4)

Hamid N K
Hamid N K

Reputation: 87

You can try this:

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

But you can not get the dropdown value in controller after submission. You can use a hidden field for setting the dropdown value.

Upvotes: 0

dougajmcdonald
dougajmcdonald

Reputation: 20037

You'd probably want to look at the override for DropDownListFor which take a dictionary of HTML attributes and perhaps pass something like 'disabled = "true"' through.

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>

Upvotes: 2

Joel Etherton
Joel Etherton

Reputation: 37533

You use the disabled attribute with values of true and false.

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

Upvotes: 9

Kapil Khandelwal
Kapil Khandelwal

Reputation: 16144

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

Upvotes: 1

Related Questions