1

I know there were a number of people that faced this issue but I don't feel like they ever resolved it.

I have a razor page like this:

@model MyModel
<div>
    @using (Html.BeginForm("MyMethod", "Home", FormMethod.Get))
    {
        @Html.CheckBoxFor(model => model.MyBool)
        @Html.LabelFor(model => model.MyBool)
        <input type="submit" class="btn" value="Fire it up" />
    }
</div>

My model looks like this:

public class MyModel
{
    [DisplayName("My checkbox")]
    public bool MyBool {get; set;}
}

And my method in the home controller looks like this:

public IActionResult MyMethod(MyModel model)
{
    return View();
}

Everything looks good to me, but then, no matter if I check the checkbox or not, the model parameter in the MyMethod method is always false after I submit the form.

What am I doing wrong?

EDIT:

When I check the checkbox the GET request contains both values - true and false. I saw some people say that MVC should handle it but it apparently doesn't.

6
  • Did your form's GET request send something like this: MyBool=true&MyBool=false? This is related to how CheckBoxFor helper generates HTML: stackoverflow.com/questions/2697299/…. Check model.MyBool inside MyMethod to confirm this behavior. Commented Mar 12, 2019 at 3:14
  • @TetsuyaYamamoto Yes, the GET request looks exactly like that. The model.MyBool value inside MyMethod is always false, regardless the checkbox state. Commented Mar 12, 2019 at 3:22
  • 1
    I tried to reproduce your issue here and found that the request URL query string looks exactly like mentioned before, but MyBool property was properly set to true. Can you provide Core MVC version to make sure? Commented Mar 12, 2019 at 3:38
  • @TetsuyaYamamoto .NET Core 2.1 Commented Mar 12, 2019 at 3:41
  • I followed these steps using a test project in my own machine: check the checkbox, submit the form, get request query string (/Home/MyMethod?MyBool=true&MyBool=false) and returns same view, the checkbox is still on checked state, not unchecked as stated in your question. The debugger also mentioned MyBool set as true after submit. Commented Mar 12, 2019 at 4:42

1 Answer 1

1

So, what happens is that when you don't check an HTML checkbox, the value sent by default is null. But when you are using the HTML helper @Html.CheckBoxFor(model => model.MyBool), it is automatically creating a Checkbox for the MyBool value as you asked, and additionally a @Html.Hidden("MyBool",false). What this will do is, if MyBool doesn't have any value (if checkbox isn't checked), it will pass False, instead of null.

HTML created:

  <input id="MyBool" name="MyBool" type="checkbox" value="true">
  <input name="MyBool" type="hidden" value="false">
Sign up to request clarification or add additional context in comments.

1 Comment

I tried this and it didn't work. The answer doesn't really tell me anything. Both checked and unchecked return false.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.