Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
@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");
                        }
                    }
                }
            }
          });
        });
Posted
Updated 3-Jun-14 21:42pm
v3
Comments
NKHari 4-Jun-14 3:06am    
$('#chkRolePrivilege' + i).attr('checked', true); This line inside the for loop is the problem...
it's checked attribute is not working but all other attributes of it are working fine

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900