How to get checkboxes checked or not on razor asp.net ?

Ahmed Abd El Aziz 310 Reputation points
2023-06-27T13:06:20.16+00:00

I have two checkbox razor html .i face issue how to detect which checkbox checked or not on page model 

<div class="col-md-8">
                        <table class="table align-items-center table-flush">
                            <thead class="thead-dark">
                                <tr>
                                    <th scope="col">Printers</th>
                                    <th scope="col"><input type="checkbox" name="all" id="all" checked /></th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <th scope="row">Tamayaz Point 3X (Gold)</th>
                                    <td>
                                        <input type="checkbox" id="cbRed"  >
                                    </td>
                                </tr>
                                <tr>
                                    <th scope="row">Tamayaz Point 2X (Silver)</th>

                                    <td>
                                        <input type="checkbox"  id="cbBlue">
                                    </td>
                                </tr>
                                <tr>
                                    <th scope="row">Tamayaz Point 1X (Green)</th>

                                    <td>
                                        <input type="checkbox"  id="cbGreen">
                                    </td>
                                </tr>
                                <tr>
                                    <th scope="row">Tamayaz Point X (Black)</th>

                                    <td>
                                        <input type="checkbox"  id="cbBlack">
                                    </td>
                                </tr>
                              
                            </tbody>
                        </table>
                    </div>

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

public class LabelPrintingModel : PageModel

{ public LabelPrintingModel() {

} Public void OnPostInsert()

{

which checkbox on razor html checked true

}

}

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,187 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,269 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 26,136 Reputation points
    2023-06-27T14:28:46.74+00:00

    You've asked this question many times. The community has explained the holes in your understanding and responded with several different approaches. These code samples range from JavaScript, tag helpers, using a hidden field, basic HTML, and basic C# logic. It would be easier to provide further assistance if you explained why the previous code samples did not work for you.

    Unfortunately, your most recent code snippet has too many issues to guess how the logic is intended to function. My best guess...

    The four checkboxes have values 3X, 2X, 1X, and X. Since the browser only submits a checked checkbox (name/value), all you have to do is check if the a corresponding bound property is null or empty. If the property is null then the checkbox was not checked.

    @page
    @model RazorPagesDemo.Pages.Forms.IndexModel
    @{
        ViewData["Title"] = "Basic HTML Forms";
    }
    
    
    <div class="col-md-8">
        <form asp-page-handler="Insert">
            <table class="table align-items-center table-flush">
                <thead class="thead-dark">
                    <tr>
                        <th scope="col">Printers</th>
                        <th scope="col"><input type="checkbox" name="all" id="all" checked /></th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <th scope="row">Tamayaz Point 3X (Gold)</th>
                        <td>
                            <input type="checkbox" name="Gold" value="3X" checked="@(!String.IsNullOrEmpty(Model.Gold))" />
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">Tamayaz Point 2X (Silver)</th>
    
                        <td>
                            <input type="checkbox" name="Silver" value="2X" checked="@(!String.IsNullOrEmpty(Model.Silver))" />
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">Tamayaz Point 1X (Green)</th>
    
                        <td>
                            <input type="checkbox" name="Green" value="1X" checked="@(!String.IsNullOrEmpty(Model.Green))" />
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">Tamayaz Point X (Black)</th>
    
                        <td>
                            <input type="checkbox" name="Black" value="X" checked="@(!String.IsNullOrEmpty(Model.Black))" />
                        </td>
                    </tr>
                </tbody>
            </table>
            <div>
                <input type="submit" name="Insert" value="Insert" />
            </div>
        </form>
    </div>
    <div>
        <div>Red Checked = @(!String.IsNullOrEmpty(Model.Gold)) Value = @Model.Gold</div>
        <div>Green Checked = @(!String.IsNullOrEmpty(Model.Silver)) Value = @Model.Silver</div>
        <div>Blue Checked = @(!String.IsNullOrEmpty(Model.Green)) Value = @Model.Green</div>
        <div>Black Checked = @(!String.IsNullOrEmpty(Model.Black)) Value = @Model.Black</div>
        @if (Model.Values != null)
        {
            <div>
                Values = @(String.Join(", ", Model.Values))
            </div>
        }
    </div>
    
    
    
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    
    namespace RazorPagesDemo.Pages.Forms
    {
        public class IndexModel : PageModel
        {
            public void OnGet()
            {
            }
    
            public void OnPostInsert() 
            { 
                if(!String.IsNullOrEmpty(Gold))
                {
                    //Gold is selected
    
                    Values.Add(Gold);
                }
                if (!String.IsNullOrEmpty(Silver))
                {
                    //Silver is selected
                    Values.Add(Silver);
                }
                if (!String.IsNullOrEmpty(Green))
                {
                    //Green is selected
                    Values.Add(Green);
                }
                if (!String.IsNullOrEmpty(Black))
                {
                    //Black is selected
                    Values.Add(Black);
                }
            }
    
            [BindProperty]
            public string? Gold { get; set; }
            [BindProperty]
            public string? Silver { get; set; }
            [BindProperty]
            public string? Green { get; set; }
            [BindProperty]
            public string? Black { get; set; }
    
            public List<string> Values { get; set; } = new List<string>();
        }
    }
    
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,686 Reputation points
    2023-06-27T18:37:16.63+00:00

    as we have told you several times, the browser will only post a checkbox if it has a name attribute. also if you don't include a value, it just posts the name, so to the server it has a null value.

    you can also use the asp-for="bindingname" which will generate a name and value="true" along with a hidden field with the same name and value="false"

    0 comments No comments