下拉列表使用jquery绑定

时间:2014-07-02 09:45:51

标签: c# jquery-ui

我想在下拉框中输入这种类型的绑定

- 任何Bachelor's度 -
BCA
BCOM

- 任何Master's度 -
MCA
MBA

- 任何学位 -
PGD​​CA

1 个答案:

答案 0 :(得分:0)

您可以在下方使用,这只是您可以根据需要进行更改的示例

function OnSuccess(data) {       
        $('#ddlAccessLevelGroup').empty();
        var d = data;
        var dropdown = $('#ddlAccessLevelGroup');
        var GroupCode = "";
        var optGroup;

        for (var i = 0; i < d.length; i++) {
            if (d[i].GroupCode.toString() != GroupCode) {
                optGroup = $("<optgroup style='background-color:#94c0d2' />");
                optGroup.attr('label', d[i].GroupCode.toString());
            }
            GroupCode = d[i].GroupCode.toString();
            optGroup.append(
                    $('<option></option>').val(d[i].AccessLvlId.toString()).html(d[i].AccessLvlName.toString())
                );
            dropdown.append(optGroup);
        }    
    }

C#代码

public IQueryable<AccessLevel> TestGetAccessLevelData()
        {
            IQueryable<AccessLevel> AccessLevelGrp = null;

            IQueryable<AccessLevel> AccessLevel = from a in unitOfWork.ACCESS_LEVEL_RXRepository.Get()
                                                         select new AccessLevel
                                                    {
                                                        AccessLvlId = a.ACCESS_LVL_ID_RX,
                                                        AccessLvlName = a.ACCESS_LVL_NAME,
                                                        GroupCode = "Group A"
                                                    };

            IQueryable<AccessLevel> AccessLevel1 = from a in unitOfWork.ACCESS_LEVEL_RXRepository.Get()
                                                         select new AccessLevel
                                                         {
                                                             AccessLvlId = a.ACCESS_LVL_ID_RX,
                                                             AccessLvlName = a.ACCESS_LVL_NAME,
                                                             GroupCode = "Group B"
                                                         };                      

            var result = AccessLevel.Union(AccessLevel1);          
            return result.OrderBy(c=> c.GroupCode);
        }