Checkbox disabled attribute in ASP.NET MVC

后端 未结 4 903
生来不讨喜
生来不讨喜 2020-12-06 09:20

My ViewModel has a property of selected and selectable. Both are boolean. I would like my view to have a checkbox that is enabled when selectable is true and disabled when

4条回答
  •  一生所求
    2020-12-06 09:35

    It is not easy to achieve this with an if condition inside the helper method because all the below markups will render a disabled chechbox.

    
    
    
    
    
    

    This should work in the razor. Simple If condition and rendering what you want.

    @if(item.Selected)
    { 
      @Html.CheckBoxFor(modelItem => item.Selected)
    }
    else
    {
        @Html.CheckBoxFor(modelItem => item.Selected, new { @disabled = "disabled"})
    }
    

    You may consider writing a custom html helper which renders the proper markup for this.

提交回复
热议问题