Model :
public IEnumerable<Role> Roles { get; set; }
Index :
Roles = securityServiceClient.GetAllRoles()
View :
@foreach (var role in Model.Roles)
{
<tr>
@if (role.Name == "User")
{
<td><input type="checkbox" checked="checked"/></td>
}
else
{
<td><input type="checkbox"/></td>
}
<td>@Html.Label(role.Name)</td>
</tr>
}
[HttpPost]
CreateSomething :
How can I get the selected checkbox(s) from the view?
You must give your checkboxes names:
and then:
Also you should be using view models, strongly typed views and helpers and not hardcode checkboxes as you did in your veiws.
Here’s what I mean:
Model:
and then:
and in the view: