aruni
aruni

Reputation: 2752

How to create read only checkbox MVC?

I'm using MVC. In the view there is check box, So I want to make it read only or user can't change it..

This is my view

     <tr id="pnHireSet">
                <td class="adminTitle">
                    @Html.LabelFor(model => model.IsHireSet):
                </td>
                <td class="adminData">
                    @Html.EditorFor(model => model.IsHireSet)

//This is check box. I want set is read only....
                    @Html.ValidationMessageFor(model => model.IsHireSet)
                </td>
            </tr>

How can I do it?? Thankx.

Upvotes: 2

Views: 6635

Answers (2)

Ananda G
Ananda G

Reputation: 2539

If you want to visualize the checkbox but turn of the control of the checkbox then simply you can try

@Html.CheckBoxFor(model => model.IsActive, new { onclick = "return false" })

I don't prefer to use disable=false because if you need the checkbox value to the server then disable always send false either it views as checked in state, that's the problem and difference.

Upvotes: 4

Andrew Cooper
Andrew Cooper

Reputation: 32576

Just use DisplayFor instead of EditorFor.

If you need the value to be included in the form submission you could add a HiddenFor as well.

<td class="adminData">
    @Html.DisplayFor(model => model.IsHireSet)
    @Html.HiddenFor(model => model.IsHireSet)
</td>

Upvotes: 16

Related Questions