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.
MyBool=true&MyBool=false? This is related to howCheckBoxForhelper generates HTML: stackoverflow.com/questions/2697299/…. Checkmodel.MyBoolinsideMyMethodto confirm this behavior.MyBoolproperty was properly set totrue. Can you provide Core MVC version to make sure?/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 mentionedMyBoolset astrueafter submit.