Skip to content Skip to sidebar Skip to footer

Conditional Html Attribute With Html Helper

I am using a Html helper to create a checkbox. Pending some condition, I want to add the disabled attribute to the htmlAttribute object. I have the following code: @if (Model.IsAut

Solution 1:

Try the below code:

@{
 var attr = new { @class = "input-class" };
 if (Model.IsAuthorized)
 {
    attr = new { @class = "input-class", @disabled = "disabled" };
 }
}
@Html.CheckBoxFor(x => @Model.Property, attr)

Post a Comment for "Conditional Html Attribute With Html Helper"