Pass Viewmodel properties to Controller

Asked

Viewed 455 times

0

I put my table inside a BeginForm passing my controller and my action, but when I give Submit the fields are NULL in the controller, I don’t think this passing the properties of Viewmodel.

@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)
    {}

inserir a descrição da imagem aqui

Error in Partial: inserir a descrição da imagem aqui the public IList<DeliveryServiceViewModel> DeliveryServices is the list populating the <table> and that I would like to move on to the controller

  • I needed the Viewmodel properties to go to the list, more specifically the list DeliveryServices that appears in the image, it contains the fields I need.

  • Related: http://answall.com/questions/181996/passar-list-para-actionresult

1 answer

2

Now it’s a case of using the Begincollectionitem. You have several answers about, but I will also pass a few steps that may be useful.

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 the Html.BeginForm normal, without Viewbags

@using (Html.BeginForm("EditAll", "DeliveryService", FormMethod.Post)) { ... }
  • edited my question with partial but for some reason I can’t install the beginCollectionItem using Install-Package BeginCollectionItem returns this error: Install-Package : An error occurred while retrieving package metadata for 'BeginCollectionItem' from source 'nuget.org'.&#xA;At line:1 char:1&#xA;+ Install-Package BeginCollectionItem&#xA;+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&#xA; + CategoryInfo : NotSpecified: (:) [Install-Package], Exception&#xA; + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand&#xA;

  • Your Nuget is not up to date, it is?

  • I think I managed to install, I edited my question with the partial and the viewModel, but it returns me an error that I also put in my question, in the other questions that you answered about the Begincollectionitem Viewmodel with a List They look just like mine.

  • Check to see if you have an entry in <namespaces> to the BeginCollectionItem in the archive Web.config inside the directory Views (not the Web.config root directory). Also check that the package is even installed correctly through Object Explorer.

Browser other questions tagged

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