Click here to Skip to main content
15,904,935 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have a model class which is having several list properties among list properties , I am displaying one list in the multi selection checkbox .

How to bind selected checkbox properties to model on selection of checkbox on post method.

ex
 public class Gadgets
{
    public int Id {get;set;}  
    public List<Mobiles> {get;set;}
    public List< Product> {get;set;}
}

public class Product
{
    public int productId {get;set;}
    public string productName {get;set;}
    public string productDescription{get;set:}
}



In my strongly typed view
<input type="checkbox" /><label>@Model.ProductName </label>

When user clicks on checkbox how to post selected product name descriptio and name to Gadget model
Posted
Updated 27-Feb-14 9:39am
v2

1 solution

try it by Using java script & hidden field for doing this.
Take hidden field in design.

HTML
 @Html.Hidden("HdIsPrivate",0)
//Checkbox control
  @Html.CheckBox("ChkPrivate", new { @onclick = "CheckIsPrivate()" }) Private

Set hidden field value using javascript.
JavaScript
<script type="text/javascript">
    function CheckIsPrivate() {
       // var CheckBox = document.getElementsByTagName('ChkBoxId');
        var x = $("#ChkPrivate").is(":checked");
       
        if (x.toString() == "true") {
           // alert('checked');
            document.getElementById('HdIsPrivate').value = '1';
        }
        else if (x.toString() == "false") {
            //alert('Unchecked');
            document.getElementById('HdIsPrivate').value = '0';
        }    

    } 

</script>


Access hidden field value in controller.

C#
int is_private = Convert.ToInt32(collection["HdIsPrivate"]);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900