Pass properties from the ViewModel to the Controller

0

I put my table inside a BeginForm passing my controller and my action, but when I submit submit the fields are NULL in the controller, I think it is not passing the ViewModel properties.

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

@using (Html.BeginForm("EditAll", "DeliveryService", FormMethod.Post))
{

<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>
                <input type="submit" value="@Resources.ViewLabelNameStrings.Save" />
                <button id="cancel">Cancelar</button>
            </div>
            <select id="situacao" disabled>
                <option selected disabled hidden>Alterar Status</option>
                <option value="realizada">Realizada</option>
                <option value="naoRealizada">Não Realizada</option>
            </select>
        </th>
        <th></th>

        @if (Roles.IsUserInRole(UserPermission.DeliveryServiceEdit))
            {
            <th></th>
        }

        @if (Roles.IsUserInRole(UserPermission.DeliveryServiceEdit))
            {
            <th></th>
        }

        @if (Roles.IsUserInRole(UserPermission.DeliveryServiceDelete))
            {
            <th></th>
        }
    </tr>
    @if (Model != null)
    {
        <tbody>
            @foreach (var deliveries in Model.DeliveryServices)
            {
                var checkboxChecked = deliveries.IsSelected;

                var carrierFullName = "Não Definido";
                if (deliveries.CarrierModel != null)
                {
                    carrierFullName = String.Concat(deliveries.CarrierModel.FirstName, " ", deliveries.CarrierModel.LastName);
                }

                var shipperCustomerFullName = "Não Definido";
                if (deliveries.ShipperCustomerModel != null)
                {
                    shipperCustomerFullName = deliveries.ShipperCustomerModel.CompanyName ?? String.Concat(deliveries.ShipperCustomerModel.FirstName, " ", deliveries.ShipperCustomerModel.LastName);
                }

                if (ViewBag.Routes.ContainsKey("id"))
                {
                    ViewBag.Routes.Remove("id");
                }
                ViewBag.Routes.Add("id", deliveries.IdDeliveryService);

                Html.RenderPartial("_DeliveryServiceEntry.cshtml", deliveries);
            }
            }
        </tbody>
    }
</table>

    <div id="IdCarrierDiv" style="display:none"></div>
    <div id="IdShipperCustomerDiv" style="display:none"></div>

}

My partial:

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

@using (Html.BeginCollectionItem("DeliveryServices"))
{
<tr>
    <td>@Html.DisplayFor(modelItem => carrierFullName)</td>
    <td>@Html.DisplayFor(modelItem => shipperCustomerFullName)</td>
    <td>@Html.DisplayFor(modelItem => deliveries.Created)</td>
    <td>@Html.DisplayFor(modelItem => deliveries.Description)</td>
    <td>@Html.DisplayFor(modelItem => deliveries.DeliveryServiceStatusModel.Description)</td>
    <td>@Html.DisplayFor(modelItem => deliveries.NonDeliveryDescription)</td>

    @*declaração de variavel local para simplificar o tratamento condicional de "description"*@
    @{var description = deliveries.DeliveryServiceStatusModel.Description;}
    @if (description == "Em Andamento" || description == "Roteirizado")
    {
        <td>@Html.CheckBoxFor(modelItem => deliveries.IsSelected)</td>
    }
    else
    {
        <td></td>
    }
    <td class="table-comand">
        <a href="@Url.Action("Details", ViewBag.Routes)" title="Detalhes" class="table-details-link"></a>
    </td>

    @if (Roles.IsUserInRole(UserPermission.DeliveryServiceEdit))
    {
        <td class="table-comand">
            <a href="@Url.Action("Edit", ViewBag.Routes)" title="Editar" class="table-edit-link"></a>
        </td>
    }

    @if (Roles.IsUserInRole(UserPermission.DeliveryServiceEdit))
    {
        <td class="table-comand">
            @if (deliveries.IdDeliveryServiceStatus == (int)Dlieve.Model.Enums.DeliveryServiceStatusEnum.Cancelado)
                {
                <a title="Entrega/Serviço cancelado" class="table-block-disabled"></a>
            }
            else
            {
                <a href="javascript:void(0)" data-cancel-delivery="@Html.DisplayFor(modelItem => deliveries.IdDeliveryService)" title="Cancelar" class="table-block-link"></a>
            }
        </td>
    }

    @if (Roles.IsUserInRole(UserPermission.DeliveryServiceDelete))
    {
        <td class="table-comand">
            <a href="@Url.Action("Delete", ViewBag.Routes)" title="Excluir" class="table-delete-link"></a>
        </td>
    }
</tr>

}

My ViewModel:

public class DeliveryServiceListViewModel
{
    public string CarrierFullName { get; set; }
    public string ShipperCustomerFullName { get; set; }

    //public string FilePath { get; set; }

    public virtual ICollection<DeliveryServiceViewModel> DeliveryServices { get; set; }

    public Dictionary<long, string> DeliveryServiceTypes { get; set; }

    public Dictionary<long, string> DeliveryServiceStatus { get; set; }
}

My action:

    [HttpPost]
    public ActionResult EditAll(DeliveryServiceListViewModel model)
    {}

PartialError: the public IList<DeliveryServiceViewModel> DeliveryServices is the list that populate <table> and that I would like to pass to the controller

    
asked by anonymous 07.02.2017 / 17:04

1 answer

2

Now is a case of using the BeginCollectionItem . You have several answers about , but I'll also take a couple of steps that might be helpful.

1. Put this in a Partial, for example, _DeliveryServiceEntry.cshtml

            <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>*@
                    <td>
                        <input id="DeliveryServices_@(i)__IsSelected" name="DeliveryServices[@i].IsSelected" type="checkbox" value="true" checked="@checkboxChecked">
                        <input id="DeliveryServices_@(i)__IdDeliveryService" name="DeliveryServices[@i].IdDeliveryService" type="hidden" value="@Model.DeliveryServices[i].IdDeliveryService">
                    </td>

                }
                else
                {
                    <td></td>
                }
                }
            </tr>

2. Use Html.BeginForm normal, without ViewBags

@using (Html.BeginForm("EditAll", "DeliveryService", FormMethod.Post)) { ... }
    
07.02.2017 / 17:18