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

后端 未结 12 1451
长发绾君心
长发绾君心 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:19

    If a checkbox is checked, then the postback values will contain a key-value pair of the form [InputName]=[InputValue]

    If a checkbox is not checked, then the posted form contains no reference to the checkbox at all.

    Knowing this, the following will work:

    In the markup code:

     
    

    And your action method signature:

    public ActionResult Index( string responsables, bool checkResp = false)
    

    This will work because when the checkbox is checked, the postback will contain checkResp=true, and if the checkbox is not checked the parameter will default to false.

提交回复
热议问题