ahmed salah

ahmed salah

  • 931
  • 547
  • 64.4k

How to get unchecked checkboxes on list using razor page ?

May 13 2023 5:33 PM

I working on razor page using .net core 7 . i face issue i can't pass check boxes uncheck to on post function on page model

I have group of checkboxes have checked false with another meaning not checked 

so How to get unchecked checkboxes as list of values

my code details 

@Html.AntiForgeryToken()
    <form method="post">
        <button id="FillCheckBox" 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" name="UserAccessViewModel.UserId" 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="UserAccessViewModel.checks" type="checkbox" asp-for="UserAccess.MODULECODE" value="1">
        <label for="lblStockTake">Stock Take</label>
    <input id="ShelfLabelPrint" name="UserAccessViewModel.checks" type="checkbox" asp-for="UserAccess.MODULECODE" value="2">
        <label for="lblShelfLabel">Shelf Label Print</label>

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

        
       
        
    </form>

on pagemodel

public async Task OnPost(UserAccessViewModel UserAccessViewModel)
{
}

view model used 

public class UserAccessViewModel
        {
            public int UserId { get; set; }

            public UserAccess userAccess { get; set; }
            public int[] checks { get; set; }
        }

 

to see expected result i need as below 


Answers (4)