Checkbox sends null to controller on form post

Hi,

I have a form that sends data to the controller save data in the database. The form is not using a model. On the form I have a checkbox. When the checkbox is checked or unchecked and the form is submitted to the controller, the checkbox parameter passed to the controller has a null value.

What do I need to do to send the bool value from the checkbox to the controller?

3 Replies 1 reply marked as answer

GK Gayathri KarunaiAnandam Syncfusion Team April 21, 2021 05:50 AM UTC

Hi Eddie, 

We have checked your reported query. We can send data to controller using ajax call. We have prepared a sample in which checked property of Checkbox is passed to controller. Please check the below code snippet. 

@using (Html.BeginForm()) 
{ 
    @Html.EJS().CheckBox("default").Label("I agree to the Terms and Conditions").Change("onChange").Render() 
} 
 
<script> 
 
    function onChange(args) { 
        $.ajax({ 
            url: "/Home/Index", 
            type: "POST", 
            datatype: "json", 
            contentType: "application/json; charset=utf-8", 
            data: JSON.stringify({ "Checked": args.checked }), 
            success: function (result) { 
 
            } 
        }); 
    } 
</script> 

[HttpPost] 
        public ActionResult Index(CheckboxState form) 
        { 
            return View(); 
        } 
        public class CheckboxState 
        { 
            public bool Checked { get; set; } 
        } 

For your reference, we have prepared a sample based on this. Please check the below link. 


Please get back to us, if you need further assistance. 

Regards, 
Gayathri K 


Marked as answer

EW Eddie Willcox April 21, 2021 09:11 AM UTC

Thank you. This is exactly what I needed


GK Gayathri KarunaiAnandam Syncfusion Team April 22, 2021 04:58 AM UTC

Hi Eddie, 

You are welcome. 

We are happy to hear that your requirement has been fulfilled. Please feel free to contact us if you need any further assistance on this. 

Regards, 
Gayathri K 


Loader.
Up arrow icon