如何使用对控制器的ajax调用从数据库表填充下拉列表?我试过这个。
AJAX致电
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; }
}
型号:
for (int idx = 0; idx < total_row_student; idx++)
答案 0 :(得分:1)
使用此:在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);
}
现在按照自己的方式进行编辑
答案 1 :(得分:1)
$.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);
}
答案 2 :(得分:1)
将您的选择查询更正为
var position: CGFloat = 0.0
for urlString in (stringURLArray).reverse() {
let imageView1 = UIImageView(frame: CGRectMake(0 + position, 0, 200, 50))
let url = NSURL(string: urlString) //
let data = NSData(contentsOfURL: url!)
imageView1.image = UIImage(data: data!)
self.view.addSubview(imageView1)
position += 200 // space out the width of each image
}