MVC 5 Checkbox returns “False, false” or “false”,

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)
{
    //Cast to bool because userInfo.isActive is Nullable<bool>
    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 /*Also tried @checked = 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)
{
    //There a lot more up here, but it not relevant to the problem
    userInfo.isActive = model.IsActive;
    var tmp = Request.Form["IsActive"]; //Added just to check the form value

    try
    {
        db.Entry(userInfo).State = EntityState.Modified;
        //Assign changed data to userInfo
        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.

, , ?!

+4
1

value . , ,

@Html.CheckBoxFor(model => model.IsActive)

<input data-val="true" data-val-required="The IsActive field is required." id="IsActive" name="IsActive" type="checkbox" value="true" class="valid">

, , Model.IsActive false, true

, CheckBox, , value=true ( ):

private static MvcHtmlString CheckBoxHelper(HtmlHelper htmlHelper, ModelMetadata metadata, string name, bool? isChecked, IDictionary<string, object> htmlAttributes)
        {
            RouteValueDictionary attributes = ToRouteValueDictionary(htmlAttributes);

            bool explicitValue = isChecked.HasValue;
            if (explicitValue)
            {
                attributes.Remove("checked"); // Explicit value must override dictionary
            }

            return InputHelper(htmlHelper,
                               InputType.CheckBox,
                               metadata,
                               name,
                               value: "true",
                               useViewData: !explicitValue,
                               isChecked: isChecked ?? false,
                               setId: true,
                               isExplicitValue: false,
                               format: null,
                               htmlAttributes: attributes);
        }
+1

Source: https://habr.com/ru/post/1606085/


All Articles