Treat Action Return in JSon Format

1

Colleagues Devs, I have an application in C # and ASP.NET with an Action that returns an array in JSON. When I return only one string I get handle with javascript now, when I return an array I do not know how to handle it in javascript.

Follow my Action:

        public ActionResult ListaAcessoUsuario(int codEqpto, string login)
    {
        var acessoDao = new AcessoDAO();
        var acessos = acessoDao.ListaAcessoUsuario(codEqpto, login);

        return Json(acessos);
    }

Follow my Html:

<table>
<thead>
    <tr>
        <th></th>
        <th>Hora Inicio</th>
        <th>Hora Fim</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>Dias Úteis</td>
        <td id="HrIniU"></td>
        <td id="HrFinU"></td>
    </tr>
    <tr>
        <td>Sábados</td>
        <td id="HrIniS"></td>
        <td id="HrFinS"></td>
    </tr>
    <tr>
        <td>Domingos</td>
        <td id="HrIniD"></td>
        <td id="HrFinD"></td>
    </tr>
    <tr>
        <td>Feriados</td>
        <td id="HrIniF"></td>
        <td id="HrFinF"></td>
    </tr>
</tbody>

<script type="text/javascript">

function atualizaCampos(Eqpto, usuario) {
    var url = "@Url.Action("ListaAcessoUsuario", "Acesso")";
    $.post(url, { codEqpto: Eqpto, login: usuario }, atualiza);
}

function atualiza(acessos) {
    //aqui quero pegar o retorno da Action que é um array e exibir na tabela                    
}
</script>
    
asked by anonymous 06.02.2018 / 11:20

1 answer

1

You can render the table body with the content, something like this:

function atualizaCampos(Eqpto, usuario) {
    var url = "@Url.Action("
    ListaAcessoUsuario ", "
    Acesso ")";
    $.post(url, {
        codEqpto: Eqpto,
        login: usuario
    }, function(data) {
        atualiza(data);
    });
}

function atualiza(data) {
    $('tbody').empty();
    data.forEach(function(item) {
        $('tbody').append('<tr><td>Dias Úteis</td><td>' + item.hinicio + '</td><td>' + item.hfim + '</td></tr>');
    });
}
    
06.02.2018 / 11:29