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

后端 未结 3 1067
闹比i
闹比i 2021-01-14 13:49

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 pr

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-14 14:40

    I ran into a similar issue. There is a form we have that contains many fields that include textboxes, select (dropdown) menus, checkboxes and of course labels. All fields save to the database properly EXCEPT for the three checkbox options we have. I just figured out the fix after fooling with it off and on for weeks. I hope this helps someone:

    This is what the code used to look like:

    $(this).attr('data-val', 'true');
    

    This is what fixed the problem:

    $(this).val(true);
    

    Here is the whole function that includes the fix:

    $('form').on('change', ':checkbox', function () {
        if (this.checked) {
            $(this).val(true);
        }
        else {
            $(this).val(false);
        }
    });
    
    

提交回复
热议问题