Javascript 使用Ajax Post在MVC中选中“获取值”复选框

Javascript 使用Ajax Post在MVC中选中“获取值”复选框,javascript,ajax,asp.net-mvc,checkbox,Javascript,Ajax,Asp.net Mvc,Checkbox,谁能帮帮我吗 这是我的代码: Index.cshtml <!DOCTYPE html> <html> <head> <title>jQuery With Example</title> @Scripts.Render("~/bundles/jquery") <script type="text/javascript"> $(function () { $('.

谁能帮帮我吗

这是我的代码:

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() {
            $.ajax({
                url: @Url.Action("Index", "Home" , "Index"),
                type: "POST",
                data: formData ,
                dataType: "json",
                success: function(data){
                    alert(data.RoleID)
                },
                error: function(e){
                    debugger;
                }
        }

</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 chkitem-1" />
            </td>
            <td>
                <input type="checkbox" class="chkitem chkitem-1" />
            </td>
            <td>
                <input type="checkbox" class="chkitem chkitem-1" />
            </td>
        </tr>
        <tr>
            <td>Free User</td>
            <td>
                <input type="checkbox" class="chkview chkview-2" />
            </td>
            <td>
                <input type="checkbox" class="chkitem chkitem-2" />
            </td>
            <td>
                <input type="checkbox" class="chkitem chkitem-2" />
            </td>
            <td>
                <input type="checkbox" class="chkitem chkitem-2" />
            </td>
        </tr>
    </table>
    <br />

        <button type="submit" class="buttons buttons-style-1" onclick="Save()">Save</button>
}

</body>
</html>
我想问两个问题

  • 如何使用ajax解析列表复选框的值?我想要解析我选中的复选框的类名,例如,如果我选中行管理员,{'chkinsert-1','chkupdate-2',我需要数组列表

  • 如何在控制器post中获取数组的值集合? 例子: 根据用户选中的复选框输入,公共操作结果索引(字符串RoleID,数组[]collChecbox)collChecbox={'chkinsert-1','chkupdate-2'}的内容


  • 有人能帮我吗???

    为什么不使用Ajax.BeginForm()这使得发送已发布表单值变得非常容易

    此外,您应该首先创建适当的模型

    模型

    在视图中执行以下操作

        @model Model.UserRole
        <div id="result"></div>
        @using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "result" }))
        {
            <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>
                        @Html.CheckBoxFor(m => m.Administrator.Checkbox1)
                    </td>
                </tr>
                <tr>
                    <td>Free User</td>
                    <td>
                        @Html.CheckBoxFor(m => m.FreeUser.Checkbox1)
                    </td>
                </tr>
            </table>
            <br />
            <button type="submit" class="buttons buttons-style-1" onclick="Save()">Save</button>
        }
    
    另外,不要忘记包括ajax库

    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
    
    
    
        @model Model.UserRole
        <div id="result"></div>
        @using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "result" }))
        {
            <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>
                        @Html.CheckBoxFor(m => m.Administrator.Checkbox1)
                    </td>
                </tr>
                <tr>
                    <td>Free User</td>
                    <td>
                        @Html.CheckBoxFor(m => m.FreeUser.Checkbox1)
                    </td>
                </tr>
            </table>
            <br />
            <button type="submit" class="buttons buttons-style-1" onclick="Save()">Save</button>
        }
    
        [HttpPost]
        public ActionResult Index(UserRole model)
        {
            return View();
        }
    
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>