Handle Json Format Action Return

Asked

Viewed 74 times

1

Devs, I have a C# and ASP.NET application with an Action that returns a JSON array. When I return only a string I can deal with javascript now, when I return an array I don’t know how to handle 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);
    }

Follows 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>

1 answer

1


You can render the table body with the contents, something like that:

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>');
    });
}
  • That’s right Lucas. Thank you very much!

Browser other questions tagged

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