Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do my checkboxes not pass their value to my controller in ASP.NET Core?

I feel this may be an easy fix but I cannot seem to get around it. I have an ASP.NET Core web application and I'm using an ajax form to submit data to my controller for processing. The problem I have is that my checkboxes always pass in as "false" even though they may be checked.

I searched for some answers but nothing I've tried has worked. Things like using the checked property, ensuring the name is correct.

Can anyone shed some light on why the values are ignored when the form is submitted to the controller and how I can fix it?

Form

<form asp-action="CreateCustomView" asp-controller="Data"
        data-ajax="true"
        data-ajax-method="POST">
        <div class="row">
            <div class="col">
                <div class="custom-control custom-checkbox">                            
                    <input type="checkbox" class="custom-control-input" id="ColName" name="ColName" checked>
                    <label class="custom-control-label" for="ColName">Name</label>
                </div>
            </div>

            <button type="reset">Reset</button>
            <button type="submit">Save changes</button>
</form>

Model

namespace MyProject.Data
{
    public class GridView : BaseEntity
    {
        public string Name { get; set; }               
        public bool ColName { get; set; }
    }
}

DataController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MyProject.Data;
using Microsoft.AspNetCore.Mvc;

namespace MyProject.UI.Controllers
{
    public class DataController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult CreateCustomView(GridView data)
        {
            //This method is incomplete, there is a break at the ActionResult to see what values are passed into `data`.  In production, the values would be handled and the changes would be saved.
            return View();
        }
    }
}

I have a breakpoint on the method CreateCustomView to see what values are passed, no matter what checkboxes I check, they are always being passed as false.

Can anyone help with this rather strange problem?

Update

As per the answer supplied by @Reyan-Chougle, if you're using CORE you need to supply the input as follows:

<input asp-for="@Model.ColType" type="checkbox" />

When the page is rendered, the control is automatically given a hidden field. Here is what the HTML of the above control renders as:

<input type="checkbox" class="custom-control-input" id="ColType" value="true" data-val="true" data-val-required="The ColType field is required." name="ColType">
<input type="hidden" value="false" id="ColType" name="ColType">

As you can see it creates a hidden checkbox with a value of false. This means that, as a boolean type it always has a value of true or false on submission.

like image 518
Yanayaya Avatar asked Dec 22 '22 22:12

Yanayaya


2 Answers

As you are using ASP.NET Core, it is recommend to use the Tag Helpers:

<div class="form-group">
    <label asp-for="@Model.ColName"></label>
    <input asp-for="@Model.ColName" type="checkbox" />
</div>
like image 74
Reyan Chougle Avatar answered Dec 28 '22 08:12

Reyan Chougle


If not using asp-for attribute , you can modify your codes to add a hidden field. It will be submitted regardless whether the checkbox is checked or not. If the checkbox is checked, the posted value will be true,false. The model binder will correctly extract true from the value. Otherwise it will be false :

<input type="checkbox" class="custom-control-input" data-val="true" id="ColName" name="ColName" value="true" checked>
<label class="custom-control-label" for="ColName">Name</label>
<input name="ColName" type="hidden" value="false">
like image 41
Nan Yu Avatar answered Dec 28 '22 08:12

Nan Yu