如何填写下拉列表表单数据库表

本文关键字:数据库 表单 下拉列表 何填写 | 更新日期: 2023-09-27 17:53:26

如何使用对控制器的ajax调用从数据库表中填充下拉列表?我试过这个。

AJAX调用

$(function () {
    $.ajax({
        type: "POST",
        url: "GetDropDown",
        data: '{}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (r) {
            alert("success");
            $.each(r.d, function () {
                alert("error");
            });
        }
    });
});

控制器代码:

public static Shelf<ShelfItems> GetDropDown()
        {
            string query = "SELECT StoreID";
            string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    List<ListItem> customers = new List<ListItem>();
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = con;
                    con.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            customers.Add(new ListItem
                            {
                                Value = sdr["CustomerId"].ToString(),
                                Text = sdr["Name"].ToString()
                            });
                        }
                    }
                    con.Close();
                }
            }
             return customers;
        }

型号:

   public class Shelf
        {
            public string StoreID { get; set; }
            public string ShelfName { get; set; }
            public string MinTemperature { get; set; }
            public string MaxTemperature { get; set; }
        }    

如何填写下拉列表表单数据库表

将您的选择查询更正为

 string query = "SELECT CustomerId,Name From yourTableName";

使用这个:在Html:中

      $.ajax({
                        type: "GET",
                        url: '@Url.Action("AllCustomer", "Customers")',
                        contentType: "application/json;charset=utf-8",
                        data: JSON.stringify(),
                        dataType: "json",
                        success: function(data) {
                            $('#customers').empty().append('<option value="">---Select---</option>');
                            $.each(data, function(index, value) {
                                $('#customers').append($('<option />', {
                                    value: value.Id,
                                    text: value.Name
                                }));
                            });
                        },
                    });

行动中:

public ActionResult AllCustomer()
{
  return Json(db.Customers.ToList(), JsonRequestBehavior.AllowGet);
}

现在按照自己的方式编辑

    $.ajax({
        type: "GET",
        url: '/ControllerName/ActionName',
        success: function (data) {
            $('#customers').empty().append('<option value="">--Select---</option>');
            $.each(data, function (index, value) {
                $('#customers').append($('<option />', {
                    text: value.StoreID
                }));
            });
        },
    });

控制器内

    public virtual JsonResult GetDropDown()
    {
        string query = "SELECT StoreID";
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        List<ListItem> customers = new List<ListItem>();
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        customers.Add(new ListItem
                        {
                            Value = sdr["CustomerId"].ToString(),
                            Text = sdr["Name"].ToString()
                        });
                    }
                }
                con.Close();
            }
        }
        return Json(customers, JsonRequestBehavior.AllowGet);
    }