Reda RACHID Reda RACHID - 6 months ago 11
Ajax Question

Displaying List returned from ASP.NET MVC Controller towards AJAX

so i am really not that good with FRONT-END side especially jQuery.
BACK-END side i am retrieving a list that is executed from a Stored Procedure
and i need to send back that list and diplay " APPEND " the list in an existing table.

BACK-END Action :

[HttpPost]
public JsonResult Search(string RefAO)
{

BD_MarcheEntities ctx = new BD_MarcheEntities();
var list = ctx.Bilan_Lot(RefAO).ToList<Bilan_Lot_Result>();
if(list != null)
{

//var li = JsonConvert.SerializeObject(list);
return Json(list, JsonRequestBehavior.AllowGet);

}
else
{
return Json("fail");
}
}


the jQuery/AJAX function :

$(document).on("click","#bt_search",function (e) {
alert("ff");
$.ajax({
url : '/Lots/Search',
type : 'POST',
data : { RefAO : $("#RefAppelOffre").val() },
success : function(list) {
if(list == "fail"){

alert('error');

}
else{


$.each(list,function(){

$("#tab tbody").append("<tr>" +
"<td>" + list.Ref_Lot + "</td>" +
"<td>" + list.Titre + "</td>" +
"<td>" + list.TotalLotTTC + "</td>" +
"<td>" + list.NombreConcurrent + "</td>" +
"<td>" + list.NombreArticle + "</td>" +
"</tr>");
})

}//Fermeture Else
}//Fermeture Success

})//fermeture ajax
})


someone please explain to me what i am doing wrong here !?

this it displays

UPDATE :
this what i am getting when using the ( console.log )

this is actually the row i am expecting to be added to the table

thank you in advance.
have a nice day

Answer

Try this

success : function(list) {
    if(list == "fail"){
        alert('error');
    }
    else{
        $.each(list,function(i){
           $("#tab tbody").append(
                "<tr>" +
                    "<td>" + list[i].Ref_Lot + "</td>" +
                    "<td>" + list[i].Titre + "</td>" +
                    "<td>" + list[i].TotalLotTTC + "</td>" +
                    "<td>" + list[i].NombreConcurrent + "</td>" +
                    "<td>" + list[i].NombreArticle + "</td>" +
                "</tr>");
        })
    }//Fermeture Else
}//Fermeture Success