I working on asp.net core MVC razor page . I face issue I can't get checkbox checked or not on page model razor page .
so which checkbox have checked or not checked this is exactly I need to get on page model MVC.
so if checkboxes StockTake and ShelfLabelPrint checked so I will get value 1 and 2 is true on page model .
checkbox Transfer not checked so it will get value 3 status false .
C#
public class AddUserAccessModel : PageModel
{
[BindProperty]
public UC.ADC.Core.Entities.SQL.UserAccess UserAccess { get; set; }
public async Task OnPost(UC.ADC.Core.Entities.SQL.UserAccess useraccess)
{
string[] checkedModuleCodes = Request.Form["checks"];
// How to recognize which check box have checked or not here
}
}ON AddUserAccess.cshtml Razor view
C#
@Html.AntiForgeryToken()
Submit
User ID
Stock Take
Shelf Label Print
Transferexpectation result I need as below


Rajkiran SwainRank #165 · 14.4K reputationPosted May 11, 2023, 10:19 AM EDT
public class AddUserAccessModel : PageModel { [BindProperty] public UC.ADC.Core.Entities.SQL.UserAccess UserAccess { get; set; } public async Task OnPost() { bool stockTakeChecked = UserAccess.MODULECODE == 1; bool shelfLabelPrintChecked = UserAccess.MODULECODE == 2; bool transferChecked = UserAccess.MODULECODE == 3; // Do something with the checked checkboxes here } }ahmed salahRank #2,240 · 628 reputationPosted May 12, 2023, 10:21 AM EDT
i get error can't set object reference to null
Rajkiran SwainRank #165 · 14.4K reputationPosted May 12, 2023, 10:44 AM EDT
Check if you have declared and initialized the variables and objects correctly.
Ensure that you have included all the necessary libraries or packages that are required for your code to work.
Verify that the values you are passing to the objects are valid and not null.
Check for any typos or syntax errors in your code.
Debug your code to locate the exact line where the error is occurring.
If the error persists, try searching online for similar issues and possible solutions.
Tuhin PaulRank #7 · 71.9K reputationPosted May 12, 2023, 10:14 PM EDT
To recognize which checkbox have checked or not
public class AddUserAccessModel : PageModel { [BindProperty] public UC.ADC.Core.Entities.SQL.UserAccess UserAccess { get; set; } public async Task OnPost(UC.ADC.Core.Entities.SQL.UserAccess useraccess) { var checkedModuleCodes = Request.Form["checks"] as string[]; if (checkedModuleCodes != null) { foreach (var checkedModuleCode in checkedModuleCodes) { switch (checkedModuleCode) { case "1": UserAccess.MODULECODE = 1; break; case "2": UserAccess.MODULECODE = 2; break; case "3": UserAccess.MODULECODE = 3; break; } } } } }This code will first get the value of the "checks" input field from the request. If the value is not null, it will then iterate through the values and set the corresponding value of the UserAccess.MODULECODE property. For example, if the user checks the "StockTake" and "ShelfLabelPrint" checkboxes, the UserAccess.MODULECODE property will be set to 1 and 2, respectively.