Getting Started with ASP.NET Core Checkbox Control

23 Jul 20263 minutes to read

This section briefly explains how to include the ASP.NET Core Checkbox control in your ASP.NET Core application using Visual Studio.

Create an ASP.NET Core Web App with Razor pages

Create an ASP.NET Core Web App using Visual Studio via Microsoft Templates or the Syncfusion® ASP.NET Core Extension. For detailed instructions, refer to the ASP.NET Core Web App Getting Started documentation.

Install the required ASP.NET Core packages

To add ASP.NET Core Checkbox control in the app, open the NuGet package manager in Visual Studio (Tools → NuGet Package Manager → Manage NuGet Packages for Solution), search for and install the Syncfusion.AspNetCore.Buttons and Syncfusion.AspNetCore.Themes packages. All Syncfusion ASP.NET Core packages are available on nuget.org. See the NuGet packages topic for details.

Alternatively, you can install the same packages using the Package Manager Console with the following command.

Install-Package Syncfusion.AspNetCore.Buttons -Version 34.1.29
Install-Package Syncfusion.AspNetCore.Themes -Version 34.1.29

Add ASP.NET Core Tag Helpers

After the packages are installed, open the ~/Pages/_ViewImports.cshtml file and import the Syncfusion.AspNetCore.Base and Syncfusion.AspNetCore.Buttons Tag Helpers.

@addTagHelper *, Syncfusion.AspNetCore.Base
@addTagHelper *, Syncfusion.AspNetCore.Buttons

Add stylesheet and script resources

The theme stylesheet and script can be referenced from CDN. Include the stylesheet and script references inside the <head> of ~/Pages/Shared/_Layout.cshtml file.

<head>
    ...
    @* ASP.NET Core controls styles *@
    <link rel="stylesheet" href="_content/Syncfusion.AspNetCore.Themes/styles/fluent2.css" />
    @* ASP.NET Core controls scripts *@
    <script src="_content/Syncfusion.AspNetCore.Buttons/scripts/sf-check-box.min.js"></script>
</head>

Register the Script Manager

Open the ~/Pages/Shared/_Layout.cshtml file and register the script manager <ejs-scripts> at the end of the <body> element as follows.

<body>
    ...
    @* ASP.NET Core Script Manager *@
    <ejs-scripts></ejs-scripts>
</body>

Add ASP.NET Core Checkbox control

Add the ASP.NET Core Checkbox control in the ~/Pages/Index.cshtml file.

<ejs-checkbox id="default" label="Default"  checked="true"></ejs-checkbox>

Run the application

Press Ctrl+F5 (Windows) or +F5 (macOS) to launch the application. The ASP.NET Core Checkbox control will render in your default web browser.

ASP.NET Core Checkbox Control

Change the Checkbox state

The Checkbox has three visual states:

  1. Checked
  2. Unchecked
  3. Indeterminate

The Checkbox checked property is used to handle the checked and unchecked states.
When checked, a tick mark is displayed in the Checkbox.

Indeterminate

The Checkbox indeterminate state can be set through the indeterminate property. The Checkbox indeterminate state masks the real value of the Checkbox visually. The Checkbox cannot be changed to the indeterminate state through the user interface; this state can be achieved only through the property.

<ul>
    <li>
        <ejs-checkbox id="checked" checked="true" label="Checked State"></ejs-checkbox>
    </li>
    <li>
        <ejs-checkbox id="unchecked" label="Unchecked State"></ejs-checkbox>
    </li>
    <li>
        <ejs-checkbox id="indeterminate" indeterminate="true" label="Intermediate State"></ejs-checkbox>
    </li>
</ul>

<style>
    .e-checkbox-wrapper {
        margin-top: 18px;
    }

    li {
        list-style: none;
    }
</style>

NOTE

View Sample in GitHub.

See also

  1. Getting Started with ASP.NET Core using Razor Pages
  2. Getting Started with ASP.NET Core MVC using Tag Helper