Razor how to create a CheckBox and make it READONLY?
I will just add my case which may be useful to someone.. if you have html attribute too in your checkboxFor then
@Html.CheckBoxFor(model => model.item, new { onclick = "return false", Title = "is not editable",htmlAttributes = new { @class = "form-control" } })
@Anupam Roy answer will work good.just adding my case.Hope it will be useful.
@Html.CheckBoxFor(model => model.IsActive, new { onclick = "return false" })
This should work. However i got a problem with @disabled = "disabled". If i make the checkbox checked by default and make the checkbox disabled at the same time it wont post with its value. At controller it will get false. Using "return false" user wont able to change the value and it will also post the value which has set on load as by default.
I'm using MVC4, you get the same effect with a simple something like
@Html.DisplayFor(m => m.IsPublished)
No need to care to select checkbox or set disabled yourself. Not sure, if this is a new feature in MVC4, so maybe it will work in MVC3 as well.
Should be something like this:
@Html.CheckBoxFor(m => m.IsPublished, new { @disabled = "disabled" })
UPDATE:
Assuming that your class AdvSlot
contains a property IsPublished
you can write in your loop:
<td>
@Html.CheckBox(item.AdvSlotId + "_IsPublished", item.IsPublished, new { @disabled = "disabled" });
</td>