根据下拉列值在ListBox中显示值

时间:2015-01-21 08:47:25

标签: asp.net-mvc asp.net-mvc-4 asp.net-ajax html.listboxfor

在我看来,我有一个下拉角色和第二个ListBox作为权限。我想更改ListBox中的值,因为用户选择下拉列表中的值

<tr> 
<td>@Html.Label("","Role")</td>
<td>@Html.DropDownListFor(x => x.Role, @ViewBag.role as SelectList, new { @class = "dropdown"} )</td>
</tr>
<tr>
<td>@Html.Label("","Permissions in Role")</td>
<td>@Html.ListBox("permissions", @ViewBag.permissions as SelectList, new { @class = "chosen-select", data_placeholder = "Add Permissions to Role...", style = "width:500px;", tabindex = "4"})</td>
</tr>

在我的js文件中(斯蒂芬建议后更新)

   $(document).on('change', '#Role_RefID', function () {
    var selection = $(this).val();
    $.getJSON(window.location.pathname + "/getPermissions?id="+selection, function (result) {
    var ddl = $('#permissions');
    ddl.empty().trigger("chosen:updated");
    $(result).each(function () {
            $(document.createElement('option'))
                .attr('value', this.Id)
                .text(this.Value)
                .appendTo(ddl)
                .attr('selected',"selected") 
            ;
        });
        ddl.trigger("chosen:updated");
    });
});

getPermissions(在Stephen Suggestion之后更新)

  public JsonResult getPermissions(int id)
    {         
        List<Reference> perms = rep.permsInRole(id);
        var res = perms.Select(p => new { Id = p.RefID, Value = p.Description }).Distinct().ToList();          
        return Json(res, JsonRequestBehavior.AllowGet);
    }

ListBox显示来自DB的初始值,但是当我更改下拉列表中的值时它不清除Listbox的内容,也不会在ListBoxListBox中显示新值

0 个答案:

没有答案