CSharp · Mvc
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace HelloWorldMvcApp
{
public class SampleViewModel
{
[Display(Name=" ")]
public bool IsChecked { get; set; }
}
}
@model HelloWorldMvcApp.SampleViewModel
@{
Layout = null;
}
<!DOCTYPE html>
<!-- template from http://getbootstrap.com/getting-started -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap 101 Template</title>
<!-- CSS Includes -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<style type="text/css">
section {
margin: 30px;
}
.slider [type="checkbox"] {
display: none;
}
.slider .toggle-label {
display: block;
width: 40px;
height: 20px;
position: relative;
background: #ed495c;
border-radius: 10px;
transition: background 0.2s ease;
cursor: pointer;
}
.slider .toggle-label::before {
content: '';
display: block;
width: 50%;
height: 100%;
background: #ffffff;
border-radius: 50%;
box-shadow: 0 0 0 1px #d1d1d1;
position: absolute;
left: 0;
top: 0;
transition: transform 0.2s ease-in-out;
}
.slider [type="checkbox"]:checked ~ .toggle-label {
background: #93ed49;
}
.slider [type="checkbox"]:checked ~ .toggle-label::before {
-webkit-transform: translateX(100%);
transform: translateX(100%);
}
</style>
</head>
<body>
<section title="HtmlHelper checkbox">
<p>Include 1?</p>
<div class="slider">
@Html.CheckBoxFor(m => m.IsChecked, new { @class = "toggle-pill" })
@Html.LabelFor(m => m.IsChecked, new { @class = "toggle-label" })
</div>
</section>
<section title="Generated checkbox">
<p>Include 2?</p>
<div class="slider">
<input class="toggle-pill" data-val="true" data-val-required="The field is required." id="IsChecked2" name="IsChecked2" type="checkbox" value="true">
<!-- <input name="IsChecked2" type="hidden" value="false"> -->
<label class="toggle-label" for="IsChecked2"> </label>
</div>
</section>
<section title="Classic checkbox">
<p>Include 3?</p>
<div class="slider">
<input type="checkbox" id="IsChecked3" name="IsChecked3" class="toggle-pill" >
<label for="IsChecked3" class="toggle-label"></label>
</div>
</section>
<!-- JS includes -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/mvc/4.0/jquery.validate.unobtrusive.min.js"></script>
<script type="text/javascript">
$('#IsChecked').click(function () {
if ($(this).is(':checked')) {
console.log("true");
}
else {
console.log("false");
}
});
$('#IsChecked2').click(function () {
if ($(this).is(':checked')) {
console.log("true2");
}
else {
console.log("false2");
}
});
$('#IsChecked3').click(function () {
if ($(this).is(':checked')) {
console.log("true3");
}
else {
console.log("false3");
}
});
</script>
</body>
</html>
using System;
using System.Web.Mvc;
using System.Collections.Generic;
namespace HelloWorldMvcApp
{
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index(bool? isChecked)
{
isChecked = isChecked ?? false;
var model = new SampleViewModel { IsChecked = (bool)isChecked };
return View(model);
}
}
}