Search results

Getting started with ASP.NET MVC Toggle Switch Button control

28 Jul 2021 / 1 minute to read

This section briefly explains about how to include a simple Switch in your ASP.NET MVC application. You can refer ASP.NET MVC Getting Started documentation page for introduction part of the system requirements and configure the common specifications.

Add Switch to the project

We are going to render Switch component in Index.cshtml page.

razor
default.cs
Copied to clipboard
@Html.EJS().Switch("default").Render()
Copied to clipboard
public ActionResult Default()
    {
            return View();
    }

Run the application

After successful compilation of your application, simply press F5 to run the application.

The following example shows a default rendering of Switch.

razor
default.cs
Copied to clipboard
@Html.EJS().Switch("default").Render()
Copied to clipboard
public ActionResult Default()
    {
            return View();
    }

Set text on Switch

This section explains how to set onLabel and offLabel texts on Switch. In the following example, onLabel is set as ON and offLabel is set as OFF.

razor
default.cs
Copied to clipboard
@Html.EJS().Switch("default").OnLabel("ON").OffLabel("OFF").Render()
Copied to clipboard
public ActionResult Default()
    {
            return View();
    }

Switch does not have text support for material themes, and does not support long custom text.

See Also