Tuesday, 17 September 2013

Multiple Checkbox selection using array and passing it to controller

$.each(college, function () {
                t = t + "<tbody><tr><td>" + sno + "</td><td>" + this.collegeName + "</td><td>" + this.Ad_cperson + "</td><td>" + this.Ad_contactNo + "</td><td>" + this.Ad_cEmail + "</td><td>
<input id=" + this.collegeCode + " type='checkbox' value=" + this.collegeCode + " /> </td></tr></tbody>";
              
                sno++;
            });




  $('#btnCommon').live("click"function () {//0
                var myarray = [];
                var i = 0;
                var action = $('#btnCommon').val();

                $('input:checkbox').each(function () {

                    if (this.checked) {
                        i = i + 1;
                        myarray.push($(this).val());
                    }


                });
                if (i > 0) {
                    if (action == "Approve") {
                        alert("approve");
                        $.ajax({//1
                            url: '@Url.Action( "ApproveColleges""Admin")',
                            data: { values: myarray.toString() },
                            type: 'POST',
                            dataType: 'json',
                            success: function (response) {//2
                                if (response.College != null) {//3
                                    college = response.College;

                                    fillGrid(college);

                                } //3
                            } //2
                        }); //1
                    }
                        //---------------------------------just for display
                               for (var j in myarray)
   {

                                 alert(myarray[j]);

                               }

            }); //0

In controller

[HttpPost]
        public ActionResult ApproveColleges(string values)
        {
                      
            return Json(new { College = obj.approveColleges(values) });

        }

In Model

public List<College> approveAgainColleges(string code)
        {

           

                string[] s = code.Split(',');


                for (int i = 0; i < s.Length; i++)
                {
                    string x = s[i];
                    conCheck();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = sqlcon;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "[ApproveAgainColleges]";
                    cmd.Parameters.AddWithValue("@code", x);

                    cmd.ExecuteNonQuery();

                }
                sqlcon.Close();

}

No comments:

Post a Comment