I have two checkbox razor html .i face issue how to detect which checkbox checked or not on page model
Printers
Tamayaz Point 3X (Gold)
Tamayaz Point 2X (Silver)
Tamayaz Point 1X (Green)
Tamayaz Point X (Black)
Normal (Black & White)
so which checkboxes of these have checked or not on page model
I need to write csharp code detect which checkboxes checked or not on page model
public class LabelPrintingModel : PageModel
{
public LabelPrintingModel()
{
}
Public void OnPostInsert()
{
//which checkbox on razor html checked true
}
}
Brahma Prakash ShuklaPosted Jun 28, 2023, 6:19 AM
To detect which checkboxes are checked or not on your Razor HTML page in your C# PageModel, you can use the
Request.Formcollection to access the form data submitted during the POST request. Here's an example of how you can check the status of the checkboxes:In the above code, the
AllCheckedproperty is used to store the state of the "all" checkbox, and theSelectedPrintersproperty is a list of the selected printer names. TheOnPostInsertmethod is triggered when the form is submitted, and it retrieves the form data usingRequest.Formand checks the checkboxes based on their names. You can then use the values stored inAllCheckedandSelectedPrintersfor further processing in your page model.Amit MohantyPosted Jun 28, 2023, 5:59 AM
try this once:
Deepak RawatPosted Jun 28, 2023, 5:35 AM
Page Model (
LabelPrintingModel):In the above example, I've introduced a
PrinterSelectionsclass to hold the checkbox selections. Each checkbox is bound to a corresponding property in thePrinterSelectionsclass using theasp-forattribute. In theOnPostInsertmethod, you can access the checkbox selections through thePrinterSelectionsobject and perform your logic based on the values of the properties.Mohamed Azarudeen ZPosted Jun 27, 2023, 1:17 PM
Hi Ahmed
you can bind the checkbox values to properties in your page model class. Here's an example: