Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Helloo... can somebody help me..

this is my Code:
Index.cshtml
<!DOCTYPE html>
<html>
<head>
    <title>jQuery With Example</title>
    @Scripts.Render("~/bundles/jquery")
    <script type="text/javascript">
        $(function () {

            $('.chkview').change(function () {
                $(this).closest('tr').find('.chkitem').prop('checked', this.checked);
            });

            $(".chkitem").change(function () {
                var $tr = $(this).closest('tr'), $items = $tr.find('.chkitem');
                $tr.find('.chkview').prop('checked', $items.filter(':checked').length == $items.length);
            });
        });

        function Save() {
           
        }

</script>
</head>
<body>
    <h2>Access Control-Home</h2>
    
    <br />
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { RoleID="RoleID" }))
{
    <input type="hidden" name="RoleID" value="1" id="RoleID" />
    <table id="mytable">
        <tr>
            <td>View</td>
            <td>Insert</td>
            <td>Update</td>
            <td>Delete</td>
        </tr>
        <tr>
            <td>Administrator</td>
            <td>
                <input type="checkbox" class="chkview chkview-1" />
            </td>
            <td>
                <input type="checkbox" class="chkitem chkinsert-1" />
            </td>
            <td>
                <input type="checkbox" class="chkitem chkupdate-1" />
            </td>
            <td>
                <input type="checkbox" class="chkitem chkdelete-1" />
            </td>
        </tr>
        <tr>
            <td>Free User</td>
            <td>
                <input type="checkbox" class="chkview chkview-2" />
            </td>
            <td>
                <input type="checkbox" class="chkitem chkinsert-2" />
            </td>
            <td>
                <input type="checkbox" class="chkitem chkupdate-2" />
            </td>
            <td>
                <input type="checkbox" class="chkitem chkdelete-2" />
            </td>
        </tr>
    </table>
    <br />
    
        <button type="submit" class="buttons buttons-style-1" onclick="Save()">Save</button>
}
    
</body>
</html>


HomeController.cs
[HttpPost]
public ActionResult Index(string RoleID, ????)
{
  var _roleID = RoleID;
  // how i can get parsing value of list checked checkbox???
  return View();
}


i want to ask 2 question.
1. how i can parsing value of list checked checkbox using ajax? i want parsing classname of checkbox which i checked example i need list of array if i checked row administrator, { 'chkinsert-1','chkupdate-2' }
2. how i can get value collection of array in controller post?
example:
public ActionResult Index(string RoleID, array[] collChecbox)

contents of collChecbox = { 'chkinsert-1','chkupdate-2'} in accordance with the user checked of checkbox input.

can somebody help me??
Posted
Comments
Oliver Bleckmann 19-May-14 3:47am    
well, most commonly you will name the inputs like <input type=checkbox name="whatever"> put a <form id="form"> tag around the whole thing and use $("#form").ajaxForm({url: 'blabla', type: 'post'}) or $("#theForm").ajaxSend(...) to submit it via post. and your done. now their state will be submitted and they can be accessed on the server by their names.

1 solution

<table id="mytable">
<tr>
<td>View</td>
<td>Insert</td>
<td>Update</td>
<td>Delete</td>
</tr>
<tr>
<td>Administrator</td>
<td>
<input type="checkbox" class="chkitem" value="1" />
</td>
<td>
<input type="checkbox" class="chkitem" value="2" />
</td>
<td>
<input type="checkbox" class="chkitem" value="3" />
</td>
<td>
<input type="checkbox" class="chkitem" value="4" />
</td>
</tr>
<tr>
<td>Free User</td>
<td>
<input type="checkbox" class="chkitem" value="4" />
</td>
<td>
<input type="checkbox" class="chkitem" value="5" />
</td>
<td>
<input type="checkbox" class="chkitem" value="6" />
</td>
<td>
<input type="checkbox" class="chkitem" value="7" />
</td>
</tr>
</table>
</body>
</html>

As of now i have done on checkbox checked


<script src="~/Scripts/jquery-2.1.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var selected = new Array();
$(document).on("click", ".chkitem", function (e) {
if ($(this).is(":checked")) {
alert($(this).val());
selected.push(this.value);
$.ajax({
type: "POST",
url: "@Url.Action("show", "Sample")",

data: { "Checkboxvalue": this.value },
async: false,
success: function (data) {
alert(data);
},
dataType: 'html',
error: function (data) {
alert("fail");
}
});
}
});

});

</script>




controller name is sample and action name is show parameter name is Checkboxvalue
 
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