ASP.NET - Actualizar tabla automaticamente

 
Vista:
sin imagen de perfil
Val: 36
Ha disminuido su posición en 2 puestos en ASP.NET (en relación al último mes)
Gráfica de ASP.NET

Actualizar tabla automaticamente

Publicado por fernando (26 intervenciones) el 19/06/2019 04:10:51
Hola, tengo la siguiente tabla:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@model IEnumerable<ProyectoPuerto.Models.IndoorMonitorViewModel>
 
@{
    ViewData["Title"] = "Monitor Depósito";
}
 
 
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.RetirementNumber)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.TruckRegistration)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Status)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.RetirementNumber)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.TruckRegistration)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Status)
                </td>
            </tr>
        }
    </tbody>
</table>

El tema que la columna Status se va actualizando, y mi idea era que se actualizara automaticamente sin necesidad de refrescar la pagina, es decir que cada "x" tiempo alla al controlador se traiga los datos y los cargue en la tabla.. hay forma de hacer lo que digo?
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 36
Ha disminuido su posición en 2 puestos en ASP.NET (en relación al último mes)
Gráfica de ASP.NET

Actualizar tabla automaticamente

Publicado por fernando (26 intervenciones) el 29/06/2019 02:09:46
Lo pude solucionar de la siguiente manera:

Vista:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
@model IEnumerable<ProyectoPuerto.Models.TruckDriverViewModel>
 
@{
    ViewData["Title"] = "Monitor Depósito";
}
 
 
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.RetirementNumber)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.HeavyIncome)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.TruckRegistration)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Telephone)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Brand)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.QuantityOfPackages)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Container)
            </th>
 
        </tr>
    </thead>
    <tbody id="dataTable">
        @foreach (var item in Model)
        {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.RetirementNumber)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.HeavyIncome)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.TruckRegistration)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Telephone)
            </td>
 
        </tr>
        }
    </tbody>
</table>
 
 
 
@section Scripts {
 
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
 
 
    <script type="text/javascript">
 
    $(document).ready(function () {
 
        refresh();
 
 
        function refresh() {
            setTimeout(function () {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "/TruckDrivers/TableExternalData",
                data: "",
                dataType: "json",
                success: function (data) {
                    //console.log(data);
                    $("#dataTable").empty();
                    for (var i = 0; i < data.length; i++) {
                        console.log(data[i]);
                        $('<tr> <td>' + data[i].retirementNumber + '</td> <td>' + data[i].HeavyIncome + ' </td> <td>' +'</td> <td>' + data[i].truckRegistration + ' </td> <td>' + data[i].telephone + '</td></tr>').appendTo("#dataTable");
                    }
 
                },
                error: function (response) {
                    alert("Error" + response.responseText);
                }
                });
                refresh();
            }, 10000);
 
        }
    });
 
    </script>
}


Controller:


1
2
3
4
5
6
public JsonResult TableExternalData()
        {
            IEnumerable<TruckDriver> allTruckDrivers = GetAllTruckDrivers();
            IEnumerable<TruckDriverViewModel> truckriversViewModel = ToTruckDriverViewModel(allTruckDrivers);
            return Json(truckriversViewModel);
        }

Espero que les sirva. Cualquier cosa estoy a la orden.
Saludos
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar