Answered by:
asp.net core razor page checkboxes model binding (always false)

Question
-
User1653152239 posted
i have a razor page in asp.net core with the following input tag:
<input asp-for="chkPref" />
in the code-behind i have:
public bool chkPref { get; set; }
so when i run the app, i can confirm in `Request.Form` that I'm getting...
checkbox checked = {true,false}
checkbox unchecked = {false}
which is expected, according to https://www.learnrazorpages.com/razor-pages/forms/checkboxes
however, that page states that the model binding will figure out that `{true,false}` is actually `true` but i'm getting `false` no matter what.
the website above alludes to the fact that this "just happens"...
If the checkbox is checked, the posted value will be
true,false
. The model binder will correctly extracttrue
from the value. Otherwise it will befalse
.but that doesn't seem to be working, or at least isn't that obvious how it works.
Wednesday, May 20, 2020 11:26 AM
Answers
-
User475983607 posted
Works for me. Demo code follows.
@page @model RazorDemo.Pages.general.CheckDemoModel @{ ViewData["Title"] = "CheckDemo"; } <h1>CheckDemo</h1> <form method="post"> <input asp-for="chkPref" /> <input id="Submit1" type="submit" value="submit" /> </form> <div> @Model.Message </div>
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; namespace RazorDemo.Pages.general { public class CheckDemoModel : PageModel { [BindProperty] public bool chkPref { get; set; } public string Message { get; set; } = string.Empty; public void OnGet() { } public void OnPost() { Message = $"The value is {chkPref}"; } } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, May 20, 2020 11:51 AM -
User1653152239 posted
problem was i was missing this...
[BindProperty]
as stated on this page...
https://www.learnrazorpages.com/razor-pages/forms#handler-method-parameters
The property to be included in model binding must be decorated with the
BindProperty
attribute.Thanks!
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, May 20, 2020 5:42 PM
All replies
-
User475983607 posted
Works for me. Demo code follows.
@page @model RazorDemo.Pages.general.CheckDemoModel @{ ViewData["Title"] = "CheckDemo"; } <h1>CheckDemo</h1> <form method="post"> <input asp-for="chkPref" /> <input id="Submit1" type="submit" value="submit" /> </form> <div> @Model.Message </div>
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; namespace RazorDemo.Pages.general { public class CheckDemoModel : PageModel { [BindProperty] public bool chkPref { get; set; } public string Message { get; set; } = string.Empty; public void OnGet() { } public void OnPost() { Message = $"The value is {chkPref}"; } } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, May 20, 2020 11:51 AM -
User1653152239 posted
problem was i was missing this...
[BindProperty]
as stated on this page...
https://www.learnrazorpages.com/razor-pages/forms#handler-method-parameters
The property to be included in model binding must be decorated with the
BindProperty
attribute.Thanks!
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, May 20, 2020 5:42 PM