如何在ajax成功中遍历通用列表 [英] how to iterate through a generic list in success of ajax

查看:196
本文介绍了如何在ajax成功中遍历通用列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ajax的成功中我想将客户列表附加到UI元素,为此我需要遍历ajax成功结果的通用客户列表。为此

我有控制器方法,如

In success of ajax I want to append list of customers to UI element, For that I need to iterate through a generic list of customers which the result of ajax success. For this
I have controller method like

public JsonResult GetCustomer(int Id)
       {
           IList<Customer> CustomerList = new List<Customer>();
           CustomerList = db.Customer.Where(b => b.Id== Id).ToList();
           return Json(CustomerList, JsonRequestBehavior.AllowGet);
       }







和,我有像abr那样的ajax >





and,I have wrtitten ajax like

$.ajax({
            type: "POST",
            url: "/Customers/GetCustomer",
            data: { Id: 1 },
            dataType: 'json',
            success: function (response) {
                for (var i = 0; i < response.CustomerList.length; i++) {
                   $("#Customer-tabs").append('<li id=li_' + i + '><a>' + response.CustomerList[i].Name+ '</a></li>');
                }                
            }
        });





以上代码出了什么问题?

请建议一个解决方案。



What is going wrong in above code?
please suggest a solution.

推荐答案

.ajax({
type: POST
url: / Customers / GetCustomer
数据:{Id: 1 },
dataType:' json'
成功: function (响应){
for var i = 0 ; i< response.CustomerList.length; i ++){
.ajax({ type: "POST", url: "/Customers/GetCustomer", data: { Id: 1 }, dataType: 'json', success: function (response) { for (var i = 0; i < response.CustomerList.length; i++) {


#顾客的选项卡)。追加(< span class =code-string>' < li id = li _' + i + ' >< a>' + response.CustomerList [i] .Name + ' < / A>< /立GT;');
}
}
});
("#Customer-tabs").append('<li id=li_' + i + '><a>' + response.CustomerList[i].Name+ '</a></li>'); } } });





以上代码出了什么问题?

请建议一个解决方案。



What is going wrong in above code?
please suggest a solution.


您的回复本身就是一个列表。查看更新的代码:

Your response is a list itself. See the updated code:


这篇关于如何在ajax成功中遍历通用列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆