At first glance, this post may look like a duplicate, but it is not. Believe me, I looked at all the stack overflows to no avail.
Anyway, I get some weird behavior from Html.CheckBoxFor.
I have a view model with this next specific property
[Display(Name = "User is active")]
public bool IsActive { get; set; }
It is initialized before presentation.
if (userInfo.isActive != null)
{
model.IsActive = (bool)userInfo.isActive;
}
ModelState.Clear();
return View(model);
Then displayed in Html.BeginForm as
<div class="form-group">
@Html.LabelFor(model => model.IsActive, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.CheckBoxFor(model => model.IsActive, htmlAttributes: new { @value = Model.IsActive })
@Html.ValidationMessageFor(model => model.IsActive)
</div>
</div>
And returned to the controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditProfile(EditProfileViewModel model, string Roles, HttpPostedFileBase ProfileImage)
{
userInfo.isActive = model.IsActive;
var tmp = Request.Form["IsActive"];
try
{
db.Entry(userInfo).State = EntityState.Modified;
db.SaveChanges();
}
}
When model.IsActive is initialized as true, and I do not uncheck the value of model.IsActive in the column of the controller true, or if I uncheck the value of model.IsActive will return as false and everything is very bad.
, model.IsActive false. , , , "false", "true, false", , . false, model.IsActive false, (. "Var tmp" ), "false, false", ", " - , ? false true, true false.
, , ?!