I created a ASP.NET MVC project using VS2013, and created a model peron as shown below:
Then copy the following code to replace the view home index.cshmtl file. When I run it, the toggle worked once when I check the checkbox, the hide worked, but when I unchecked, it would not show. Anyone please help? I checked around and see similar issue but no real answers.
Person.cs as shown below:
using System;
using System.Linq;
namespace checkbox.Models
{
public class Person
{
public bool IsActive { get; set; }
public string Email { get; set; }
}
}
Below is the code for index page index.cshtml:
@model checkbox.Models.Person
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Person</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div id="myCheckbox" class="form-group">
@Html.LabelFor(model => model.IsActive, htmlAttributes:
new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.IsActive)
@Html.ValidationMessageFor(model => model.IsActive, "",
new { @class = "text-danger" })
</div>
</div>
</div>
<div id="ShowHideMe" class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes:
new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email,
new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "",
new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<script type="text/javascript">
$(function() {
$('#myCheckbox').change(function() {
$('#ShowHideMe').toggle($(this).is(':checked'));
});
});
</script>