Wednesday 13 July 2022

MVC - Fix Error - CS0428: Cannot convert method group 'GetValueOrDefault' to non-delegate type 'bool'. Did you intend to invoke the method?


Watch this example on YouTube:

 


To fix it 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.GetValueOrDefault, new { id = item.EmployeeID })

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

        }

    }

}

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 })

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

        }

    }

}

No comments:

Post a Comment