Create a list with selected table values with checkbox

Asked

Viewed 599 times

0

I have this código, that he catches the id selected from table:

 $("#tablepesquisaclientes tr").each(function (index, el) {
var $linha = $(el);
var checked = $linha.find('.checked:checked').length;
if (checked === 1) {
    console.log($linha.find('td').eq(1).text());
}
});

But I need to create a list, to receive on controller, I have already created the class, and I have stated in ViewModel

public class ListaContrato {
    public int Id { get; set; }
}

 public IList<ListaContrato> ListaContrato { get; set; }

This is the html of my table

<table class="table table-responsive table-hover table-striped" id="tablepesquisaclientes" style="font-size:12px;">
    <thead>
        <tr>
            <th></th>
            <th>Nº Contrato</th>
            <th>Cliente</th>
            <th>Valor</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.Contratos)
        {
            <tr>
                <td><input type="checkbox" class="checked"/></td>
                <td>@item.Id</td>
                <td>@item.Cliente.Nome</td>
                <td>@item.Valor.ToString("N2")</td>
                <td style="display:none;">@item.PedidoVenda.EmpresaID</td>
            </tr>
        }
    </tbody>
</table>

How can I pass the selected values to the controller ? Is there any way to go straight to the controller creating a list, or just going through ajax ?

This is the part of controller, I wanted to get it all for ViewModel if you had any way.

[HttpPost]
    public async Task<IActionResult> EnvioLote(EnviarLoteViewModel model)

I tried to create also according to Leonardo’s reply:

  public int[] Ids { get; set; }

And then on click of button doing so:

 var enviarLote = { Ids: [] };
            $("#tablepesquisaclientes tr").each(function (index, el) {
                var $linha = $(el);
                var checked = $linha.find('.checked:checked').length;
                if (checked === 1) {

                    var id = $linha.find('td').eq(1).text();
                    enviarLote.Ids.push(id);
                    console.log($linha.find('td').eq(1).text());
                }
            });
            console.table(enviarLote);
            $.post("/NFSe/EnvioLote", enviarLote)
                .done(function () {
                    console.log('postado');
                })
                .fail(function (data) {
                    console.log(data);
                    console.log('erro');
                })
                .always(function (data) {
                    console.log(data);
                    console.log('fim');
                });

But in the controller the Ids continue null.

  • To send to the controller, either you submit a form or do an Ajax.

  • Currently I am doing by submit, but how can I pass this list I will create when selecting the checkbox ?

  • Put a name in array checkboxes, type: name="ids[]"... and also put a value with the ids: value="@item.Id"... thus, when you submit, you will receive the values of checkboxes marked in the form of an array called ids.

  • @Sam this way I could not, or I did not understand the concept, I tried to pass with the ids and also with the list I created ListaContratos[] but it does not pass any kind of value.

1 answer

1


Whereas your model looks like this:

public class EnviarLoteViewModel
{
    public int[] Ids { get; set; }
}

This javascript will make the post.

$(document).ready(function () {
    $("#enviarBtn").click(function () {

        var enviarLote = { Ids: []};

        $("#tablepesquisaclientes tr").each(function (index, el) {
            var $linha = $(el);
            var checked = $linha.find('.checked:checked').length;
            if (checked === 1) {

                var id = $linha.find('td').eq(1).text();
                enviarLote.Ids.push(id);
                console.log($linha.find('td').eq(1).text());
            }
        });

        $.post("/Home/Enviar", enviarLote)
            .done(function () {
                console.log('postado');
            })
            .fail(function (data) {
                console.log(data);
                console.log('erro');
            })
            .always(function (data) {
                console.log(data);
                console.log('fim');
            });
    });
});
  • I need to show the one I already show, and through it, create a new list, with those selected by checkbox

  • I made the change to keep the javascript code you already have. ;-)

  • I tried to do as you did, but the Ids comes null, did exactly as you gave the example.

  • Up the code here - https://github.com/ltreze/questaostackoverflow - Run there and tell me, pls.

Browser other questions tagged

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