Reputation: 291
I am using a the following code to display users and their roles.
I am using knockout where I have two ObservableArray in my viewModel : members and roles.
I cannot find a way to send the username and rolename to the Roles.IsUserInRole(username, role) method inside the nested foreach loop.
What can I do to send these roles to each member in my model ?
<table>
<thead>
<tr>
<th>Username
</th>
<th data-bind="foreach: roles">
<span data-bind="text: roleName"></span>
</th>
</tr>
</thead>
<tbody data-bind="foreach: members">
<tr>
<td data-bind="text: userName">
</td>
<td data-bind="foreach: roles">
@using (Html.BeginForm("UserToRole", "Main"))
{
<input type="hidden" data-bind="value: members().userName" name="username" />
<input type="hidden" data-bind="value: roleName" name="rolename" />
<input type="checkbox" name="ischecked" onclick="this.form.submit();" value="true" checked="@System.Web.Security.Roles.IsUserInRole(username.value, rolename.value)" />
}
</td>
</tr>
</tbody>
The problem is : @System.Web.Security.Roles.IsUserInRole(username.value, rolename.value)
Upvotes: 0
Views: 265
Reputation: 4073
You can add additional property to your model and fill it by invoking IsUserInRole on server side during getting that data:
public ActionResult GetData()
{
var data = ...
foreach (var role in data.Roles)
{
role.HasRole = Roles.IsUserInRole(username, role.RoleName);
}
return Json(data);
}
Upvotes: 1