Enable CheckBox with DisplayFor

3.6k views Asked by At

I have a web application made with ASP.NET MVC 5. In this application I have a view which display a list of information.

In this list I display a boolean information with @Html.DisplayFor(modelItem => item.valide). So a CheckBox is displayed and it's checked and disabled.

How can I do to remove the disabled="disabled" from the CheckBoxes ?

2

There are 2 answers

0
Dan Nguyen On BEST ANSWER

try

@Html.EditorFor(modelItem => item.valide)

or

@Html.CheckBoxFor(modelItem => item.valide)

instead

0
Salah Akbari On

If you want to have an enable checkbox with DisplayFor then you need to have a template.

Create a DisplayTemplates folder under Shared folder an add a partial view to it and name it something for example (_myTemplate) and then paste the following code to it:

@model bool
@Html.CheckBoxFor(c => c)

Then you can have an enable CheckBox when using DisplayFor helper (just don't forget to specify the template that you've created):

@Html.DisplayFor(c => c.Valide, "_myTemplate")