How to fix Checkbox value always give 'false' in jquery.serializeJSON plugin
By FoxLearn 2/18/2024 1:05:19 AM 311
This post shows you how to de-serialize JSON data of check box values or how to fix 'checkbox value always give 'false' in jquery.serializeJSON plugin'
ASP.NET Core
<input type="checkbox" asp-for="Active" class="form-control" />
ASP.NET MVC
@Html.CheckBoxFor(m => m.Active)
Your html should be generated as shown below.
<input type="checkbox" class="form-control" data-unchecked-value="false" data-val="true" data-val-required="The Active field is required." id="Active" name="Active" value="true">
You can de-serialize by using serializeJSON as shown below.
var obj = $('form').serializeJSON(); console.log(obj);
For example
function addEdit(form) { $.validator.unobtrusive.parse(form); if ($(form).valid()) { var model = $(form).serializeJSON(); $.ajax({ type: 'POST', url: '/api/todo', data: JSON.stringify(model), contentType: 'application/json', success: function (response) { if (response.success) { //do something } } }); } return false; }
You will get true when checked and false (from data-unchecked-value attribute) when you using $('form').serializeJSON();
- How to fix 'DbContextOptionsBuilder' does not contain a definition for 'UseSqlServer'
- How to fix Can't find Request.GetOwinContext in Web API
- How to fix Can't use Server.MapPath in ASP.NET MVC
- Unable to resolve service for type 'Microsoft.AspNetCore.Identity.RoleManager'
- ASP.NET MVC Responsive Templates Free Download
- How to upload file in ASP.NET MVC
- How to Create Contact Form Flat Responsive in ASP.NET MVC
- HTTP Error 500.30 ASP.NET Core app failed to start
Categories
Popular Posts
Material Lite Admin Template
11/14/2024
Freedash bootstrap lite
11/13/2024
RuangAdmin Template
11/17/2024
Responsive Animated Login Form
11/11/2024