Checkbox disabled attribute in ASP.NET MVC

后端 未结 4 912
生来不讨喜
生来不讨喜 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:42

    Sorry, my previous answer was wrong.

    The input-element will be disabled as soon as it gets the attribute disabled. It doesn't matter if the value is true, false. In HTML you can't set disabled to false.

    So you will have to set the disabled attribute only when the condition is valid.

    something like:

    object attributes = null;
    if (!item.Selectable)
    {
        attributes = new { disabled = "disabled"};
    }
    @Html.CheckBoxFor(modelItem => item.Selected, attributes)
    

提交回复
热议问题