Disable checkbox in view

I am trying to disable a checkbox in my view using the Model property. However, in both cases, the check box is disabled. Should I use "" in the following code?

<%= Html.CheckBoxFor(c => c.HasList, new { disabled = (Model.CanModifyList) ? "" : "disabled" })%>
+3
source share
2 answers

Even if you set it disabled="", it is still considered disabled since the element will still have an attribute disabled. Without using JavaScript / JQuery, you will need to make an if statement in the view.

Amaze me when I use Razor syntax, but it should be something like:

<%if (model.CanModifyList) { %>
<%= Html.CheckBoxFor(c => c.HasList)%> 
<% } else { %>
<%= Html.CheckBoxFor(c => c.HasList, new { disabled = "disabled" })%>
<% } %>

, HTML- (, CheckBoxFor), HTML, , :)

+3

@mattytommo

@{
    if (model.CanModifyList) 
    { 
        @Html.CheckBoxFor(c => c.HasList)%> 
    } 
    else 
    { 
        @Html.CheckBoxFor(c => c.HasList, new { disabled = "disabled" })
    }
}
0

All Articles