How do I get the contents of an HTML Table and convert it to a C # List?

0

I have a html table that is created dynamically by the user. I need to get all the data inserted by it into the table and generate a List so I can save it later to a database.

Project data follows:

public class Procedimento
{
    string cnes;
    string cbo;
    string profissional;
    string procedimento;
    int idade;
    int quant;
}

Table code:

<table id="tabelaProducao">
    <tbody>
        <tr>
            <th style="text-align:center;">CNES</th>
            <th style="text-align:center;">CBO</th>
            <th style="text-align:center;">Profissional</th>
            <th style="text-align:center;">Procedimento</th>
            <th style="text-align:center;">Idade</th>
            <th style="text-align:center;">Quant</th>
        </tr>
        <tr>
            <td>7559267</td>
            <td>352210</td>
            <td>RENATA CARVALHO DA SILVA</td>
            <td>0101050062</td>
            <td>0</td>
            <td>18</td>
        </tr>
        <tr>
            <td>7559267</td>
            <td>352210</td>
            <td>JOSE SILVANIR DA ROCHA</td>
            <td>0101050062</td>
            <td>0</td>
            <td>18</td>
        </tr>
    </tbody>
</table>

I think using Json should work, but I still do not know how to do it.

Data will be received via POST and Controller will read this data and save to the database.

    
asked by anonymous 10.07.2017 / 20:31

1 answer

4

Place classes in each column, call each row and encapsulate the class.

HTML:

<table id="tabelaProducao">
    <thead>
        <tr>
            <th style="text-align:center;">CNES</th>
            <th style="text-align:center;">CBO</th>
            <th style="text-align:center;">Profissional</th>
            <th style="text-align:center;">Procedimento</th>
            <th style="text-align:center;">Idade</th>
            <th style="text-align:center;">Quant</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class="cnes">7559267</td>
            <td class="cbo">352210</td>
            <td class="profissional">JOSE SILVANIR DA ROCHA</td>
            <td class="procedimento">0101050062</td>
            <td class="idade">0</td>
            <td class="quant">18</td>
        </tr>
    </tbody>
</table>

JS:

function encapsularDadosTabela() {
    var listaTabela = [];
    $.each($("#tabelaProducao > tbody > tr"), function (index, value) {
        var linhaTabela = $(this);
        var itemTabela = {
            cnes: linhaTabela.find(".cnes").val(),
            cbo: linhaTabela.find(".cbo").val(),
            profissional: linhaTabela.find(".profissional").val(),
            procedimento: linhaTabela.find(".procedimento").val(),
            idade: linhaTabela.find(".idade").val(),
            quant: linhaTabela.find(".quant").val()
        };
        listaTabela.push(itemTabela);
    });
    return listaTabela;
};

function chamadaAjax(){
    $.ajax({
        url: "URLAQUI",
        type: "POST",
        data: { model: encapsularDadosTabela() },
        success: function (data) {
        }
    });
};

CONTROLLER:

[HttpPost]
public ActionResult SuaAction(IEnumerable<Procedimento> model)
{
    return View();
}
    
10.07.2017 / 21:20