Pass values of checkBox to controller action in asp.net mvc4

后端 未结 12 1530
长发绾君心
长发绾君心 2020-12-14 00:31

I want to test if the checkbox is checked or not from my action method, what i need is to pass checkbox value from view to controller.

This is my view:



        
12条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-14 01:28

    If you want your value to be read by MVT controller when you submit the form and you don't what to deal with hidden inputs. What you can do is add value attribute to your checkbox and set it to true or false.

    MVT will not recognize viewModel property myCheckbox as true here

    but will if you add

    Script that does it:

    $(document).on("click", "[type='checkbox']", function(e) {
            if (this.checked) {
                $(this).attr("value", "true");
            } else {
                $(this).attr("value","false");}
        });
    

提交回复
热议问题