I am developing a CRUD app in ASP.NET MVC, i have already implemented the security part which works with AD and now i want to make easier the assignment of the varius permission to the users. All groups and users are stored in a database they are bound by this relationship:
Below my current view, this does't work it is only graphic.
Here my view code:
<body>
<div class="container">
<br />
<table class="table table-responsive table-bordered">
@{
//inizialize my ef context
dev_labelsEntities db = new dev_labelsEntities();
//get all users stored in the db
var users = from u in db.Users
orderby u.username
select new
{
u.username
};
//get all groups from the db
var groups = from g in db.Groups
orderby g.group_name
select new
{
g.group_name
};
<tr>
<th>Username</th>
@foreach (var item in groups)
{
<td>@Html.DisplayFor(model => item.group_name)</td>
}
</tr>
foreach (var item in users)
{
int count = 1;
<tr>
<td>@Html.DisplayFor(model => item.username)</td>
@foreach (var itemG in groups)
{
//for distinguish the varius checkbox i use the name
//of the group and a generic count which indicates
//the row number
<td>@Html.CheckBox(""+itemG.group_name+"_"+count)</td>
}
</tr>
count++;
}
}
</table>
</div>
</body>
The final result which i expect is: when the user check a checkbox, call insert operation in UserGrops table, so a user can have more then one group (only if the user belong to Admins group, all other checkboxes must be disabled), whereas when the user unchech the checkbox delete from the table the corresponding user with the specific permission (ex, the user Jonh belong to pc and printer groups, i uncheck the checkbox which correspond to the printer group, this entry will be delete from the table and so Jonh can access only to the pc part).
If someone has advices or tips of any kind for implementing it, I'll appreciate.
Leo
EDIT
I've add to my view this script as @Amine suggest:
$(document).ready(function () {
$('.chkclass').click(function () {
var getchkid = $(this).attr('id');
var isChecked = $('#' + getchkid).is(':checked');
if ($('#' + getchkid).is(':checked') == true) {
// the value that i need
var username = $(this).attr('username');
var groupname = $(this).attr('groupname');
//ajax for calling my action medthod
$.ajax({
type: 'Post',
url: '/UserGroups/ManagePermission',
cache: false,
dataType: 'Json',
data: { usr: username, grp: groupname },
//success: function (data) {
//},
//error: function (xhr, ajaxOptions, thrownError) {
// alert(thrownError);
//}
})
}
});
});
The method receives the right value but after a simple test this happen
Before:
After:
Why this behavior?