Pass List for ActionResult

0

Briefly: I have a screen with a table populated by the list below, where I select the line through checkbox , I need to pass that list to my actionResult .

As I do not have a form on this screen I can not give the data to pass to actionResult .

ViewModel used in html with property list

    public class DeliveryServiceListViewModel
{
     public List<DeliveryServiceViewModel> DeliveryServices { get; set; }
}

ActionResult :

public ActionResult EditAll()
{
}

What I call that way I do not know if it's the right one:

<button onclick="location.href='@Url.Action("EditAll", "DeliveryService")'">
    Salvar
</button>
  • How to pass the list so I can deal with controller ?
  • Is there any way I can just pass the list or do I have to pass all the ViewModel that contains the list?

I tried to pass the list using new { DeliveryServices = Model.DeliveryServices } , but it arrives at controller empty.

 @model Dlieve.BackOffice.Areas.BackOffice.Models.DeliveryServiceListViewModel

 <table class="table-long" id="deliveries">
    <tr>
        <th>Portador</th>
        <th>Cliente</th>
        <th>Data de Cadastro</th>
        <th>Descrição</th>
        <th>Status</th>
        <th>Motivo Entrega Não Realizada</th>
        <th><input type="checkbox" id="selectAllCheckBoxes" /></th>
        <th colspan="4">
            <div class="btnSituacao" hidden>
                <button onclick="enviarFormulario()">Salvar</button>
                <button id="cancel">Cancelar</button>
            </div>
        </th>
        <th></th>
    @if (Model != null)
    {

        for (int i = 0; i < Model.DeliveryServices.Count; i++)
        {
            var checkboxChecked = Model.DeliveryServices[i].IsSelected;

            <tr>
                <td>@Html.DisplayFor(modelItem => carrierFullName)</td>
                <td>@Html.DisplayFor(modelItem => shipperCustomerFullName)</td>
                <td>@Html.DisplayFor(modelItem => Model.DeliveryServices[i].Created)</td>
                <td>@Html.DisplayFor(modelItem => Model.DeliveryServices[i].Description)</td>
                <td>@Html.DisplayFor(modelItem => Model.DeliveryServices[i].DeliveryServiceStatusModel.Description)</td>
                <td>@Html.DisplayFor(modelItem => Model.DeliveryServices[i].NonDeliveryDescription)</td>

                @*declaração de variavel local para simplificar o tratamento condicional de "description"*@
                @{var description = Model.DeliveryServices[i].DeliveryServiceStatusModel.Description;}
                @if (description == "Em Andamento" || description == "Roteirizado")
                {
                    <td>@Html.CheckBoxFor(modelItem => Model.DeliveryServices[i].IsSelected)</td>
                }
                else
                {
                    <td></td>
                }
                        }
                    }

</table>
    
asked by anonymous 03.02.2017 / 20:01

2 answers

1

This does not fire a POST :

<button onclick="location.href='@Url.Action("EditAll", "DeliveryService")'">
    Salvar
</button>

Incidentally, I did not understand what the purpose of using button was. Anyway, if you really need JavaScript to POST to you, you will have to switch to:

<button onclick="enviarFormulario()">
    Salvar
</button>

And then:

function enviarFormulario()
{
    $.post("@Url.Action("EditAll", "DeliveryService")", { campo1: $('#campo1'), campo2: $('#campo2'), campo3: $('#campo3') }, function(result) {
        // Faça alguma coisa aqui com result
    });
}

This example uses jQuery .

EDIT

The idea is as follows:

function enviarFormulario()
{
    var lista = [];
    $(":checkbox").each(function(index) {
        lista.push({ "Campo1DoDeliveryServiceViewModel": "Valor1",
                     "Campo2DoDeliveryServiceViewModel": "Valor2",
                     "Campo3DoDeliveryServiceViewModel": "Valor3" });
    });

    $.post("@Url.Action("EditAll", "DeliveryService")", lista, function(result) {
        // Faça alguma coisa aqui com result
    });
}

I believe you want to send the value of checkboxes to the Controller . It may be necessary to fill in something with the Id and its value of checkbox .

    
03.02.2017 / 22:00
2

Your problem apparently is because you are not giving the submit in the form and simply by redirecting to the action at the click of the button. With this information will not be passed to the controller because the post is not being done, so your list goes nil.

    
03.02.2017 / 21:48