Search results

Submit name and value in form in ASP.NET MVC Toggle Switch Button control

30 Jul 2021 / 1 minute to read

The name attribute of the Switch is used to group Switches. When the Switches are grouped in form, the checked items value attribute will post to the server on form submit. The disabled and unchecked Switch values will not be sent to the server on form submit.

In the following code snippet, USB and Wi-Fi in the checked state, and Bluetooth is in disabled state. Values that are in checked state only be sent on form submit.

razor
default.cs
Copied to clipboard
<div id='container'>
<form>
    <table class='size'>
        <tr>
            <td class='lSize'>USB</td>
            <td>
                @Html.EJS().Switch("switch1").Checked(true).Render()
            </td>
        </tr>
        <tr>
            <td class='lSize'>Wi-Fi</td>
            <td>
                @Html.EJS().Switch("switch2").Checked(true).Render()
            </td>
        </tr>
        <tr>
            <td class='lSize'>Bluetooth</td>
            <td>
                @Html.EJS().Switch("switch3").Render()
            </td>
        </tr>
        <tr>
            <td>
                @Html.EJS().Button("button").Content("SUBMIT").Render()
            </td>
        </tr>
    </table>
</form>
</div>

<style>

button {
  margin: 20px 0 0 5px;
}

.size tr td {
  padding: 10px;
}

.size .lSize {
  font-family: "Roboto", "Segoe UI", "GeezaPro", "DejaVu Serif", "sans-serif";
  font-size: 13px;
}

.size .lSize label {
  user-select: none;
} 
</style>
Copied to clipboard
public ActionResult Default()
    {
            return View();
    }