I have two checkbox razor html .i face issue how to detect which checkbox checked or not on page model
Code
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
C#
public class LabelPrintingModel : PageModel
{
public LabelPrintingModel()
{
}
Public void OnPostInsert()
{
//which checkbox on razor html checked true
}
}
Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted Jun 28, 2023, 2:19 AM EDT
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:public class LabelPrintingModel : PageModel { public bool AllChecked { get; set; } public List SelectedPrinters { get; set; } public LabelPrintingModel() { SelectedPrinters = new List(); } public void OnPostInsert() { AllChecked = Request.Form["all"].Count > 0; // Check if "all" checkbox is checked foreach (var key in Request.Form.Keys) { if (key.StartsWith("cbprinter")) // Check checkboxes with name starting with "cbprinter" { var checkboxValue = Request.Form[key]; if (checkboxValue.Count > 0) { var printerName = key.Substring(9); // Extract printer name from checkbox name SelectedPrinters.Add(printerName); } } } // Now you can use the AllChecked and SelectedPrinters properties as needed } }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.Mohamed Azarudeen ZRank #297 · 8.5K reputationPosted Jun 27, 2023, 9:17 AM EDT
Hi Ahmed
you can bind the checkbox values to properties in your page model class. Here's an example:
public class LabelPrintingModel : PageModel { public bool IsRedChecked { get; set; } public bool IsBlueChecked { get; set; } public bool IsGreenChecked { get; set; } // Other checkbox properties public LabelPrintingModel() { } public void OnPostInsert() { // Check the values of the checkbox properties if (IsRedChecked) { // The "Red" checkbox is checked } if (IsBlueChecked) { // The "Blue" checkbox is checked } if (IsGreenChecked) { // The "Green" checkbox is checked } // Perform other actions based on the checkbox values } }Amit MohantyRank #112 · 20K reputationPosted Jun 28, 2023, 1:59 AM EDT
try this once:
public void OnPostInsert() { string[] selectedCheckboxes = Request.Form["cbprinter"]; foreach (string checkboxValue in selectedCheckboxes) { // Do something with the checked checkbox value Console.WriteLine("Checkbox checked: " + checkboxValue); } }Deepak RawatRank #646 · 3.7K reputationPosted Jun 28, 2023, 1:35 AM EDT
Tamayaz Point 3X (Gold) SubmitPage Model (
LabelPrintingModel):public class LabelPrintingModel : PageModel { public PrinterSelections PrinterSelections { get; set; } public LabelPrintingModel() { PrinterSelections = new PrinterSelections(); } public void OnPostInsert() { // Access the checkbox selections in the PrinterSelections object if (PrinterSelections.CbRed) { // Checkbox with id="cbRed" is checked } // Access other checkboxes in a similar way // Perform your logic based on the checkbox selections } } public class PrinterSelections { public bool CbRed { get; set; } // Add properties for other checkboxes }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.