You cannot escape if :
The problem is related to the special nature of the disabled attribute, because there is no special value that would make your sample work because :
"disabled" is the only possible value for this attribute. If the input should be enabled, simply omit the entire attribute.
So, you need to omit the attribute to enable the control, but all HTML helpers serialize all the properties of anonymous objects passed as html attributes. And there is no way to conditionally add properties to anonymous types.
However, if you have several common attributes for the enable / disable case, and you do not want to create two types of anonymoues, you can put the attributes in a dictionary with the optional disabled attribute and use the dictionary as htmlAttributes:
var checkboxHtmlAttributes = new Dictionary<string, object> {{"attibute1", "value1"}, {"attribute2", "value2"}}; if (!item.IsActive) { checkboxHtmlAttributes.Add("disabled", "disabled"); } @Html.CheckBoxFor(m=>item.IsSelected, checkboxHtmlAttributes)
nemesv
source share