从arraylist填充选择选项列表

时间:2011-10-28 18:41:04

标签: jquery sql-server select

function BindRolesDropdownList() {
        $.ajax({
            type: "Post",
            url: "Dashboard.aspx/PopulateSelectRoleList",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {

                $('#ddlRoles').get(0).options.length = 0;
                $("#ddlRoles").get(0).options[0] = new Option("Select Role", "-1");

                $.each(msg.d, function(index, item) {
                    $("#ddlRoles").get(0).options[$("#ddlRoles").get(0).options.length] = new Option(item.Display, item.Value);
                });
            }
        });
    }
    function BindDropdownList() {
        BindRolesDropdownList();
    }


     [WebMethod]
            public static ArrayList PopulateSelectRoleList()
            {
                //ArrayList lst = new ArrayList();
                //lst = DataAccess.DataAccess.GetRolesArrayList();
                //for (int i = 0; i < lst.Count; i++)
                //{


                //}


                //return lst;
                return new ArrayList()
                {

                    new { Value = 1, Display = "Male" },
                    new { Value = 2, Display = "Female" }
                };


            }

    public static ArrayList GetRolesArrayList()
            {
                ArrayList aryList = new ArrayList();
                DataSet ds = new DataSet();
                ds = DBUtility.SQLExecuteDataset("select * from ST_Roles");
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    aryList.Add(row);
                }
                return aryList;
            }

以上是填充我的选择选项的代码,我的问题是如何遍历arraylist并从数据库返回值而不是在我评论过的代码部分中传递静态值。

1 个答案:

答案 0 :(得分:0)

[WebMethod]
public static ArrayList PopulateSelectRoleList()
{
  return DataAccess.DataAccess.GetRolesArrayList()
    .Select((role, index) => new
    {
        Value = index + 1,
        Display = role
    });
}