Search code examples
javascriptjqueryasp.net-mvcasp.net-corerazor-pages

How to get checkbox have checked false or not checked on post method on mvc razor page?


I work on mvc razor page I face issue I can't get checkbox not checked or have checked false on post method on page model csharp .

Only I can get selected checkbox true .

@Html.AntiForgeryToken()
    <form method="post">
    <button id="FillCheckBox" type="submit" class="col-sm-1 btn btn-primary">Submit</button>
    <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>

I use view model as below

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

on page model asp.net razor

public async Task OnPost(UserAccessViewModel UserAccessViewModel)
        {
 //How to get transfer checkbox not checked that have checked false
//How to get list of unchecked checkbox list 
        }

checkbox have checked false not display

Expected Result Return not checked checkboxes on list

so if i have multi check box not check so return it on list this exactly what I need

Updated post

to summarize what i need

suppose i have 5 checkbox

3 checkboxes is checked true

2 checkboxes is checked false

code above posted return only 3 checked

but 2 checkbox is checked false not returned

I need to get list of checkboxes not checked so 2 checkbox not checked not retuned on list


Solution

  • It seems you are not using ASP.NET MVC, but ASP.NET Razor Pages. If so, check this guide for binding checkboxes:

    https://www.learnrazorpages.com/razor-pages/forms/checkboxes