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

Tuesday, 6 April 2021

MSSQL - How to increase length of existing varchar

Watch this example on YouTube


ALter Table Test01
Alter Column RealName Varchar(100) 

MSSQL - Rename column in table

Watch this example on YouTube


exec sp_rename 'TableName.CurrentColumnName', 'NewColumnName', 'COLUMN' 

MSSQL - Rename table

 

Watch this example on YouTube

 

 

 exec sp_rename 'CurrentTableName' , 'NewTableName'

Monday, 5 April 2021

MSSQL - Fix Error - Column names in each table must be unique. Column name 'IsValid' in table 'Test1' is specified more than once.

 Watch this example on YouTube


To fix it replace

 Alter Table Test1
Add IsValid bit not null Default 0

with  


IF COL_LENGTH('dbo.Test1', 'IsValid') Is Null
Begin

Alter Table Test1
Add IsValid bit not null Default 0
End

MSSQL - Add column only if it doesn't exist

 Watch this example on YouTube


 

IF COL_LENGTH('dbo.Test3', 'IsValid') is null
Begin
    Alter Table Test3 Add IsValid bit not null default 0
End

Select * from Test3

MSSQL - Fix Error - ALTER TABLE only allows columns to be added that can contain nulls, or have a DEFAULT definition specified, or the column being added is an identity or timestamp column, or alternatively if none of the previous conditions are satisfied the table must be empty to allow addition of this column. Column 'IsValid' cannot be added to non-empty table 'Test1' because it does not satisfy these conditions.

 Watch this example on YouTube


 

To fix it replace

 Alter Table Test1
Add IsValid bit not null

 with  

Alter Table Test1
Add IsValid bit not null Default 0

Thursday, 1 April 2021

MVC5 - Add modal (popup dialog)

 Watch this example on YouTube

 1. View

@{
    ViewBag.Title = "Home Page";
}
<div class="AddPopUp">
    @Html.Label("Add pop up")
</div>
<div class="modal fade" id="MyPopUp" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                here are going your controls etc
            </div>
        </div>
    </div>
</div>

@section Scripts{
    <script type="text/javascript">
        $(document).ready(function () {
            $(".AddPopUp").click(function (e) {
                $('#MyPopUp').modal('show');
            });
        });
    </script>
    }


2. CSS

.modal-content{
    height: 500px;
    width: 700px;
}

Friday, 19 March 2021

MVC - Simplest Extension Method example

 Watch this example on YouTube


 1. Model

   public interface IExtensionTest
    {
         void DoSomething();
    }
    public class ExtensionTest: IExtensionTest
    {
        public void DoSomething() { }
    }
    public static class SomethingElse {
        public static string ReturnSomething(this IExtensionTest t, string s)
        {
            return "returning: " + s;
        }
    }


2. Controller

        public ActionResult Index()
        {
            IExtensionTest e = new ExtensionTest();
            var res = e.ReturnSomething("bbbbb");
            return View();
        }

MVC - C# - Check if DateTime is today

 Watch this example on YouTube


            DateTime d = Convert.ToDateTime("1/1/2000");
            if (d != DateTime.Today)
            {
                // do something here.
            }

MVC - C# - Check if time is greater than something

 Watch this example on YouTube


 

            TimeSpan startSomething = new TimeSpan(8, 0, 0);
            TimeSpan endSomething = new TimeSpan(16, 0, 0);
            TimeSpan now = DateTime.Now.TimeOfDay;
            if ((now > startSomething) && (now < endSomething))
            {
                //do something here
            } 

MVC - C# - Check if today is Friday (check todays day name)

 Watch this example on YouTube


         if (System.DateTime.Now.DayOfWeek == DayOfWeek.Friday)
            {
                // do somehting here
            }

MVC - C# - Convert Now to Universal Time UTC

 

 Watch this example on YouTube


 

           String t = Convert.ToDateTime(DateTime.Now).ToUniversalTime().ToString("yyyy/MM/dd HH:mm UTC");
 

 

MVC - C# - Format Date

Watch this example on YouTube

 

 

MVC - C# - Format Date