我想获取所选复选框值的列表。我该怎么做?我正在开发一个ASP.NET MVC 5应用程序。这个程序有一个基本形式。
我的观看代码
@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" />
}
我的表单模型如下所示:
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;
}
我的控制器代码:
答案 0 :(得分:0)
复选框的名称应该相同,以便在列表中绑定。
答案 1 :(得分:0)
你可以用HttpPost做到这一点,你也可以用JQuery
来做到这一点var checkedArray=
$('input:checkbox').each(function () {//iterate all checboxes in page
var data={ "IsChecked": this.checked ,"Name":this.attr('name')}
checkedArray.push();
});
然后通过ajax
将checkedArray发送到控制器