Pass List to Actionresult

Asked

Viewed 409 times

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 this list to my actionResult.

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

Viewmodel used in html with the list property

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

ActionResult:

public ActionResult EditAll()
{
}

Which I call that I don’t know if it’s the right one:

<button onclick="location.href='@Url.Action("EditAll", "DeliveryService")'">
    Salvar
</button>
  • How to pass the list to me on controller?
  • It has as I pass only the list or I have to pass all the Viewmodel that contains the list?

I tried to pass the list using new { DeliveryServices = Model.DeliveryServices }, but she 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>
  • Your question is confused. A button submit having a Javascript meaningless in my view, seems to be the problem.

  • I removed the Ubmit, the button calls to ActionResult but I do not know how to pass my list as parameter.

  • post your form complete....?

2 answers

2

Your problem apparently is because you are not giving Ubmit in the form but simply redirecting to the action at the click of the button. With this the information will not be passed to the controller because the post is not being done, so your list arrives null.

  • You’re right, I edited my question.

1


This doesn’t fire a POST:

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

By the way, I don’t understand what the point of using the button so. Anyway, if you really need Javascript to do the POST for you, you’ll have to change 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
    });
}

That example uses jQuery.


EDIT

The idea is this:

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. You may need to fill in something with the ID and its value checkbox.

  • Actually I don’t need him to do it for me, I edited my question I think it became clearer. What I don’t know how to do is to get the list that populates my table and move on to the actionResult.

  • So. That doesn’t change the content of my answer at all. Implement the steps I put in and you’ll see the list materialized correctly in Controller.

  • I did it the way you said it and the call went through actionResult, but my list, which populates my <table>, which is a property of my Viewmodel, I don’t know how to pass by $.post

  • It is, then. You will have to build the object using Javascript. Editing your question and placing the HTML/Razor from <table>, I can put something together for you.

  • I got it, I edited it by putting the <table>

  • @Vinicius See if it’s clearer now.

Show 2 more comments

Browser other questions tagged

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