I want to get list of selected checkbox values. How can I do that?I'm working on an ASP.NET MVC 5 app. 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: