Load a table dynamically with ajax

Asked

Viewed 2,126 times

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:

inserir a descrição da imagem aqui

  • How’s the return of yours controller?

  • 3

    Note that on this line cols += "</td>"; you are closing the element td, when it should be a tr

  • It is true @Randrade, had not repaired... I fixed, but the error continues. the controller is returning the registry list of the database correctly

  • Post an example if possible. In the console (F12) an error appears?

  • 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

  • put a console.log(data) above var cols = "";. Could be the return format of controller, so I find it interesting to add your controller also

  • @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; }

  • You are returning a View(), and not a list. If your View() List has only the table, you only need to make one .html(data) instead of for(). If you want to use the For(), you wouldn’t return one View() and yes a JSON. Post as is your View() List that I will elaborate the two answers

  • could show what is bringing in the data? console.log(date);

  • @Douglasjuliao data = "<! DOCTYPE html> <html> <head> <meta name="viewport" /> <title>Horde listing</title> <link rel="stylesheet" href="https://ma

  • 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

Show 6 more comments

1 answer

2

In his Controller you are returning a View, but in his Ajax you’re waiting for a list.

A solution would only be to return the list to the View, with this, create an Action to return the JSON. In this case I called ListaJson() just so you understand better, but can put the name you want.

public ActionResult ListaJson() { 
     var lista = ListinerHorde();
     return Json(lista , JsonRequestBehavior.AllowGet);
} 

Once done, just change your URL Ajax, in this way:

 url: "/Horde/ListaJson/",

Now, in case you want to return to View, you do not need to mount the table on the front end, but add the returned HTML, an example would be this:

$.ajax({
     type: "POST",
     url: "/Horde/List/",
     success: function (data) {
        //Coloque aqui o local onde a View deverá ocupar
        $("#table_horde").html(data);
     },
     error: function (ex) {
         alert("Erro: " + ex.status);
     }
});

Just remember to change the $("#table_horde").html(data); to the place where the View should occupy.

  • I put it the way you said, but he doubled my page... data = "<! DOCTYPE html> <html> <head> <meta name="viewport" /> <title>Horde listing</title> <link rel="stylesheet" href="https://ma

  • 1

    Yes, because you are returning a full page. A View that is the code Ajax is the same as the one you are calling (Horde/List)?

  • Yes it is the same view, but I do not only load the table, there are other things inside my page tbm... registration area...

  • 1

    Then, you won’t get what you want in the same way. Create a new Action by returning Json in the form of my reply and change the url of your Ajax. Doing this, should work normally

  • @Mfilho_19 I edited the answer

  • But if I change and make it work that way, won’t he lose the two links that are on the record? In each row I have an update and delete link, if I just call a list by the controller, it will not insert these two links, it is not?

  • 1

    It will enter normally. What you need to see is how you are working with dynamic events. But test and tell me the result

  • Nothing... Add the entire page only shows 0 Namehorde "Test" Limit 23

  • 1

    @Mfilho_19 Post as is your View in the question

  • I got it!!!! Tava missing add dataType: "json", hehehe Thanks brother!!

  • 1

    @Mfilho_19 I forgot this detail, Sorry.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.