My page CSHTML variable for text and number work fine, but I cannot seem to get the check box value to post ( I am using bootstrap ), Razor pages, ASP.NET Core 2.2
I stripped my original model dialog down and the OnPost handler to try and isolate it
public class IndexModel : PageModel
{
public void OnGet()
{
}
[BindProperty]public bool customCheck1 { get; set; }
public void OnPostLineNumber(bool customCheck1) // I have tried both int and bool
{
string Message = $"Handler fired for {customCheck1}";
}
}
Best Regards,
Rena
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Then public void OnPostLineNumber(int id,string ATA,
bool doRemoval)
{
string Message = $"Handler fired for {id} with {ATA} and Removal Requested {
doRemoval}" ; // NOW WORKS
I am still puzzled why the 2 other variables
id, ATA- with just name attribute linked from the CSHTML worked but doRemoval required all the extra plumbing.
I am still puzzled why the 2 other variables
id, ATA- with just name attribute linked from the CSHTML worked but doRemoval required all the extra plumbing.
Because you need to set default value for checkbox like below:
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
27 Points
88 Posts
OnPost for checkbox variable type or why never changes
Oct 23, 2019 12:58 AM|andrewcw|LINK
My page CSHTML variable for text and number work fine, but I cannot seem to get the check box value to post ( I am using bootstrap ), Razor pages, ASP.NET Core 2.2
I stripped my original model dialog down and the OnPost handler to try and isolate it
<form asp-page-handler="LineNumber" method="post">
<div class="modal-body">
<div class="form-group">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck1" name="customCheck1">
<label class="custom-control-label" for="customCheck1">Check this custom checkbox</label>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<input type="submit" value="Go" class="btn btn-primary">
</div>
</form>
with this on CSHTML.CS
public void OnPostLineNumber(bool customCheck1) // I have tried both int and bool
{
string Message = $"Handler fired for {customCheck1}";
}
? Ideas - Thanks
Contributor
2720 Points
874 Posts
Re: OnPost for checkbox variable type or why never changes
Oct 23, 2019 03:31 AM|Rena Ni|LINK
Hi andrewcw,
You could use [BindProperty] attribute.Here is a simple workaround like below:
1.Index.cshtml:
@page @model IndexModel <form asp-page-handler="LineNumber" method="post"> <div class="modal-body"> <div class="form-group"> <div class="custom-control custom-checkbox"> <input type="checkbox" asp-for="customCheck1" class="custom-control-input"> <label class="custom-control-label" for="customCheck1">Check this custom checkbox</label> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <input type="submit" value="Go" class="btn btn-primary"> </div> </div> </form>
2.Index.cshtml.cs:
Best Regards,
Rena
Member
27 Points
88 Posts
Re: OnPost for checkbox variable type or why never changes
Oct 23, 2019 04:12 AM|andrewcw|LINK
Hi Rena
My OnPost was from a moda;Dialog that contains also a string , an int.
When I added your suggestion
<div class="custom-control custom-checkbox">
<input type="checkbox" asp-for="doRemoval" class="custom-control-input" id="doRemoval" name="doRemoval">
<label class="custom-control-label" for="doRemoval">Check for Removal, Default unchecked ...</label>
</div>
and added it to other bind variables
[BindProperty(SupportsGet = true)]
public bool doRemoval { get; set; }
Then public void OnPostLineNumber(int id,string ATA, bool doRemoval)
{
string Message = $"Handler fired for {id} with {ATA} and Removal Requested { doRemoval}" ; // NOW WORKS
I am still puzzled why the 2 other variables id, ATA - with just name attribute linked from the CSHTML worked but doRemoval required all the extra plumbing.
But it fixed the problem !!! Thank you !!!
Contributor
2720 Points
874 Posts
Re: OnPost for checkbox variable type or why never changes
Oct 23, 2019 04:46 AM|Rena Ni|LINK
Hi andrewcw,
Because you need to set default value for checkbox like below:
<form asp-page-handler="LineNumber" method="post"> <div class="modal-body"> <div class="form-group"> <div class="custom-control custom-checkbox"> <input type="checkbox" class="custom-control-input" id="doRemoval" name="doRemoval" value="true"> <label class="custom-control-label" for="customCheck1">Check this custom checkbox</label> </div> </div> <input type="text" id="id" name="id"> <input type="text" id="ATA" name="ATA"> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <input type="submit" value="Go" class="btn btn-primary"> </div> </div> </form>
Best Regards,
Rena