1
I am trying to build a table by making a request to the server and populate it with the return.
The server return is correct, but when you enter the function(data)
, I realized he carries my entire page html
, I tried to play the ajax
right in the $(document).ready
, and then use the shape below, using the load
, however, neither are working.
$("#table_horde").Load(
$.ajax({
type: "POST",
url: "/Horde/List/",
success: function (data) {
var cols = "";
for (var i = 0; i < data.length; i++) {
cols += "<tr>";
cols += "<td>" + data[i].NomeHorda + "</td>";
cols += "<td>" + data[i].Limit + "</td>";
cols += "<td><a href='#' onclick='atualizar'(" + data[i].IdHorde + ")' data-target='#janelaHordaCadastro' data-toggle='modal'>Atualizar</a></td>";
cols += "<td><a href='#' onclick='excluir'(" + data[i].IdHorde + ")'>Excluir</a></td>";
cols += "</td>";
}
$("#table_horde tbody").empty();
$("#table_horde tbody").append(cols);
$("#qtdRegistro").html(data.length);
},
error: function (ex) {
alert("Erro: " + ex.status);
}
})
);
Man Controller
is as follows:
public ActionResult List() {
return View("List", ListinerHorde());
}
private List<HordeList> ListinerHorde() {
var list = new List<HordeList>();
HordeRepository hr = new HordeRepository();
foreach (var h in hr.FindAll()) {
var model = new HordeList();
model.NameHorde = h.NameHorde;
model.Limit = h.Limit;
list.Add(model);
}
return list;
}
See below the return image:
How’s the return of yours
controller
?– Randrade
Note that on this line
cols += "</td>";
you are closing the elementtd
, when it should be atr
– Randrade
It is true @Randrade, had not repaired... I fixed, but the error continues. the controller is returning the registry list of the database correctly
– Mfilho_19
Post an example if possible. In the console (F12) an error appears?
– Randrade
I call this method in the controller and in the console no error appears, on the contrary... me status 200. But I think my problem is here "Function(data)", ta loading all my DOM in this function I can’t post the code, it’s very big @Randrade
– Mfilho_19
put a
console.log(data)
abovevar cols = "";
. Could be the return format ofcontroller
, so I find it interesting to add yourcontroller
also– Randrade
@Randrade public Actionresult List() { Return View("List", Listinerhorde()); } Private List<Hordelist> Listinerhorde() { var list = new List<Hordelist>(); Horderepository hr = new Horderepository(); foreach (var h in hr.Findall()) { var model = new Hordelist(); model.Namehorde = h.Namehorde; model.Limit = h.Limit; list.Add(model); } Return list; }
– Mfilho_19
You are returning a
View()
, and not a list. If yourView()
List has only the table, you only need to make one.html(data)
instead offor()
. If you want to use theFor()
, you wouldn’t return oneView()
and yes a JSON. Post as is yourView()
List that I will elaborate the two answers– Randrade
could show what is bringing in the data? console.log(date);
– Douglas Juliao
@Douglasjuliao data = "<! DOCTYPE html> <html> <head> <meta name="viewport" /> <title>Horde listing</title> <link rel="stylesheet" href="https://ma
– Mfilho_19
The problem then is in Controller itself, you need to bring a JSON in the answer, not to come this html, ma look at your foreach of the list
– Douglas Juliao