简体   繁体   English

获取asp.net mvc中所选复选框值的列表

[英]get list of selected checkbox value in asp.net mvc

I want to get list of selected checkbox values. 我想获得所选复选框值的列表。 How can I do that?I'm working on an ASP.NET MVC 5 app. 我该怎么做?我正在开发一个ASP.NET MVC 5应用程序。 This app has a basic form. 这个程序有一个基本形式。 在此输入图像描述

My view code 我的观点代码

@using (Html.BeginForm())
{
    <table class="table">
        <thead>
            <tr>
                <th>Field  Name</th>
                @foreach (var field in Model.Actions)
                {
                    <th>@field.Name</th>
                }
            </tr>
        </thead>
        <tbody>

            @foreach (var permission in Model.PermissionUserField)
            {
                <tr>
                    <td> @permission.Field.Name</td>
                    @foreach (var peruserAction in permission.PermissionUserFieldActions)
                    {
                        <td>
                            @if (peruserAction.Active)
                            {
                                @Html.HiddenFor(x=> x.PermissionUserFieldForUser)

                                <input type="checkbox" checked name="field_@(permission.FieldId)_[@peruserAction.Id]" />
                            }
                            else
                            {
                                <input type="checkbox" name="field_@(permission.FieldId)_[@peruserAction.Id]" />
                            }
                        </td>
                    }

                </tr>
            }

        </tbody>
    </table>

    <br />

    <input type="submit" value="Save" />

}

The model for my form looks like the following: 我的表单模型如下所示:

 public class NewViewModel
    {
        public List<PermissionUserField> PermissionUserField{ get; set; }
        public List<PermissionUserField> PermissionUserFieldForUser { get; set; }
        public List<Action> Actions { get; set; }
        public List<Field> Field { get; set; }

    }

 public partial class Action
    {
        public int Id { get; set; }

        [StringLength(50)]
        public string Name { get; set; }

    }

    public partial class PermissionUserField
    {
        public int Id { get; set; }
        public int UserId { get; set; }
        public User User { get; set; }
        public int FieldId { get; set; }
        public Field Field { get; set; }    
        public List<PermissionUserFieldAction> PermissionUserFieldActions { get; set; }
    }
    public partial class PermissionUserFieldAction
    {
        public int Id { get; set; }
        public int ActionId { get; set; }
        public Action Action { get; set; }
        public int PermissionUserFieldId { get; set; }
        public PermissionUserField PermissionUserField { get; set; }
        public bool Active { get; set; } = false;
    }

My controller code: 我的控制器代码:

复选框的名称应该相同,以便在列表中绑定。

you can do that with HttpPost, also you can do this with JQuery 你可以用HttpPost做到这一点,你也可以用JQuery做到这一点

var checkedArray=
 $('input:checkbox').each(function () {//iterate all checboxes in page
        var data={ "IsChecked":   this.checked ,"Name":this.attr('name')}
      checkedArray.push();

        });

and then send checkedArray to controller via ajax 然后通过ajax将checkedArray发送到控制器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM