Wednesday 13 July 2022

MVC - How to make a read only checkbox


Watch this example on YouTube:

 


Replace 

@model WebApplication3.Models.EmployeeList


@using (Html.BeginForm(null, null, FormMethod.Post))

{

    if (Model.myEmployees != null)

    {

        foreach (var item in Model.myEmployees)

        {

            @Html.CheckBoxFor(modelItem => item.IsValid.Value, new { id = item.EmployeeID})

            @Html.DisplayFor(modelItem => item.FirstName)

            <br />

        }

    }

}

with


@model WebApplication3.Models.EmployeeList


@using (Html.BeginForm(null, null, FormMethod.Post))

{

    if (Model.myEmployees != null)

    {

        foreach (var item in Model.myEmployees)

        {

            @Html.CheckBoxFor(modelItem => item.IsValid.Value, new { id = item.EmployeeID, @disabled = "true"})

            @Html.DisplayFor(modelItem => item.FirstName)

            <br />

        }

    }

}

No comments:

Post a Comment