Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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);
       }



and,I have wrtitten ajax like

JavaScript
$.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.
Posted
Comments
Palash Mondal_ 29-Sep-15 10:40am    
What is wrong with your current code? Are you getting any error or exception? Put a console.log(response) inside the success method? What are you getting for it in the browser console?

1 solution

Your response is a list itself. See the updated code:
JavaScript
$.ajax({
            type: "GET",
            url: "/Customers/GetCustomer",
            data: { Id: 1 },
            dataType: 'json',
            success: function (response) {
                for (var i = 0; i < response.length; i++) {
                   $("#Customer-tabs").append('<ul><li id="li_'" i="" mode="hold" /></ul>                }                
            }
        });

-KR
 
Share this answer
 
v2
Comments
Member 10374419 29-Sep-15 10:18am    
hi -KR
thanks for suggestion
But your solution causes "500 (Internal Server Error)" error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900