Friday 15 July 2022

MVC - Display Nullable checkbox


Watch this example on YouTube:

 


Replace

    @Html.CheckBoxFor(x => x.MyQuestions)

with


    @Html.CheckBox("MyQuestions", Model.MyQuestions ?? false)

MVC - Custom Validate Checkbox - Get checkbox value in JavaScript (client)


Watch this example on YouTube:

 


1. Validation 

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)

        {

            var rule = new ModelClientValidationRule

            {

                ValidationType = "validatecheckbox",

                ErrorMessage = "Fix it!"

            };

            rule.ValidationParameters.Add("fields", string.Join(",", _fields));

            yield return rule;

        }


2. JS

$.validator.addMethod('validatecheckbox', function (value, element, params) {

    debugger


    var isValid = true;

    if ($(params)[0].fields[1].val() == "True"){



        if ($(params)[0].fields[0][0].checked == false) {

            isValid = false;

        }

    }

});

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 />

        }

    }

}

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)

        }

    }

}

Fix Error CS0266 Cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?)


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

        }

    }

}

CS0119 'Employee List' is a type, which is not valid in the given context

Watch this example on YouTube:


 To fix this error replace (semicolon) in view

@model WebApplication3.Models.EmployeeList;

with

@model WebApplication3.Models.EmployeeList

Tuesday 12 July 2022

MSSQL - Check if varchar contains specified characteer



 


Declare @Test varchar(100) = 'aaa@aaa';


IF @Test Like '%@%'

BEGIN

  print ' contains @'

END

ELSE

BEGIN

print ' doesn''t contain @'

END