ahmed salah

ahmed salah

  • 1.2k
  • 547
  • 62.9k

How to get which checkbox have check true or false on razor page mvc ?

May 11 2023 1:00 PM

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 .

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

@Html.AntiForgeryToken()
    <form method="post">
        <button  type="submit" class="col-sm-1 btn btn-primary">Submit</button>
        
        <div class="form-group row">
            <label for="user-id" class="col-sm-1 col-form-label" style="font-size:15px;font-family: 'Open Sans', sans-serif;font-weight: bold;">User ID</label>
            <div class="col-sm-3">
                <input id="useraccess-id"  asp-for="UserAccess.UserId" type="text" class="form-control" style=" margin-left:10px;font-size:15px;font-family: 'Open Sans' , sans-serif;font-weight: bold;" />
            </div>
        </div>
    <input id="StockTake" name="checks" type="checkbox" asp-for="UserAccess.MODULECODE" value="1">
        <label for="lblStockTake">Stock Take</label>
    <input id="ShelfLabelPrint" name="checks" type="checkbox" asp-for="UserAccess.MODULECODE" value="2">
        <label for="lblShelfLabel">Shelf Label Print</label>

    <input id="Transfer" name="checks" type="checkbox" asp-for="UserAccess.MODULECODE" value="3">
        <label for="lblTransfer" style="margin-right:5px;">Transfer</label>

        
       
        
    </form>

expectation result I need as below


Answers (4)