@using (Html.BeginForm(null, null, FormMethod.Post, new { Id = "form" }))
{
@Html.DropDownList("Roles",newSelectList((IEnumerable<EmployeeManagementSystem.DAO.RoleMaster>)ViewData["Roles"], "RoleId", "RoleName"), "Select Role", new { @id = "ddlRole" })
<table id="sample_editable_1">
<tbody>
@{
var i = 0;
foreach (var item in Model)
{
<tr>
<td style="display: none">@Html.HiddenFor(modelItem => item.privilegeId, new { @id = "hdnPrivilegeId" + i })
</td>
<td>@Html.CheckBox("CheckBox",false, new { @id = "chkRolePrivilege" + i ,@name="PrivilegeCheckBox"})
</td>
<td>@Html.LabelFor(modelITem => item.privilegeName, item.privilegeName, new { @id = "privilegeName" + i, Value = item.privilegeName })
</td>
</tr>
i++;
}
}
</tbody>
</table>
}
I need to check the checkboxes according to the privileges of the role.But the checkboxes are not checked/unchecked inside the change event of dropdown using jquery. But the check boxes able to be checked inside the $(document).ready(function() but when we do it inside a change event or something its checked attribute is not working and all other attribute such as disabled etc are working fine.
$("#ddlRole").change(function () {
var roleId = 0;
var privilegeListName = "";
var row = $(this).parent().parent();
roleId = $('#ddlRole :selected').val();
$.ajax({
url: "/api/Privilege/GetPrivilegesByRoleId?RoleId=" + roleId,
type: 'Get',
contentType: 'application/json; charset=utf-8',
success: function (data) {
for (var i = 0; i < 4; i++) {
for (var j = 0; j < 4; j++) {
if ($('#sample_editable_1 tr #privilegeName' + i).text() == data[j].privilegeName.trim()) {
$('#chkRolePrivilege' + i).attr('checked', true);
alert("true");
}
else {
alert("false");
}
}
}
}
});
});