【问题标题】:populate select option list from arraylist从 arraylist 填充选择选项列表
【发布时间】:2011-10-29 03:05:52
【问题描述】:
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,如果您查看下面代码中的注释部分,则返回类型为arraylist。我希望我从数据库中获得的数组列表采用该格式 return new ArrayList() { new { Value = 1, Display = "Male" }, new { Value = 2, Display = "Female" } };跨度>
  • 使用 jQuery.ajax 调用动作/页面以获取以 json 形式返回的值列表。这就是我要开始的地方。

标签: jquery sql-server select


【解决方案1】:
[WebMethod]
public static ArrayList PopulateSelectRoleList()
{
  return DataAccess.DataAccess.GetRolesArrayList()
    .Select((role, index) => new
    {
        Value = index + 1,
        Display = role
    });
}

【讨论】:

  • 以上在我的选择选项列表中没有显示任何内容,我已经尝试过了。我只想遍历数组列表并将其转换为格式 return new ArrayList() { new { Value = 1, Display = "Male" }, new { Value = 2, Display = "Female" } };如何做到这一点
  • 我们可以遍历jquery中的arraylist并从那里返回结果吗?而不是在后面的代码中进行
  • 啊,您只需稍微调整GetRolesArrayList 即可返回List&lt;string&gt; 而不是ArrayList
  • Re:不是“在代码后面做”,我看到您真正要求的只是一种方法,可以将您的 Web 服务返回的 json 属性名称与 JavaScript 中的属性名称结合起来。我已经提供了。您当然可以通过对 JavaScript 稍作更改来避免整个 .Select 部分代码,包括使用计数器客户端生成索引(而不是读取 item.Value)。
  • 我找到了答案,我只需要在 GetRolesArrayList() 方法 aryList.Add(row); 中更改一行即可到 aryList.Add(new { Value = row["RoleID"].ToString(), Display = row["RoleName"].ToString() });它工作正常
猜你喜欢
  • 1970-01-01
  • 2012-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-24
  • 1970-01-01
  • 2015-10-06
  • 1970-01-01
相关资源
最近更新 更多