Creating a collection

Asked

Viewed 222 times

1

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

The two images are as mine array is. In the first image I have an array with 3 items, being the first of the month of April and contains 4 passages. For each passage I have the respective participants. My problem is in sending this array for my view, because I can’t get some properties at runtime like passagem.participante.

I tried to split it three ways arrays, but when executed via foreach on the screen is not displayed properly with the month and its participants with their tickets.

How to make an array only that I could access all the properties and play them on the screen?

Follow the view today, but I need to synchronize the information as I said before.

@using WebProvider.BMW_Incentivo.Domain.Models
@model WebProvider.BMW_Incentivo.Admin.Models.GerenciaPassagemModel

@{
    ViewBag.Title = "Gerenciamento de Passagens";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<link rel="stylesheet" type="text/css" href="@Url.Content("~/Content/css/pageDataTables.css")" media="screen" />
<link rel="stylesheet" type="text/css" href="@Url.Content("~/Content/css/tableDataTables.css")" media="screen" />
<script src="@Url.Content("~/Content/js/jquery.dataTables.js")" type="text/javascript"></script>
<script src="~/Content/js/pages/GerenciadorPassagem.js"></script>

@using (Html.BeginForm("Index", "GerenciaPassagem", FormMethod.Post))
{
    <fieldset id="passagem">

        <div style="float: left;">
            <label>Mês:</label>

            <select id="ListaPeriodo" name="ListaPeriodo" onchange="listaPassagensMes();">
                @foreach (var item in Model.ListaPeriodo)
                {
                    <option id="@item.Mes" value="@item.Mes">@item.DescricaoMes</option>
                }
            </select>

            <span class="error">* Campo Obrigatório</span>

        </div>
        <div style="float: right;">
            <label>Ano:</label>

            <select id="ListaAno" name="ListaAno">

                @*@foreach (int item in Model.Ano)
                {*@
                    <option id="@Model.Ano " value="@Model.Ano "> @Model.Ano</option>

                @*}*@
            </select>



            <span class="error">* Campo Obrigatório</span>

        </div>
        <table border="2" class="data display datatable" id="tabFiltra">

            <thead>
                <tr>
                    <th><span>Acao</span></th>
                    <th><span>Participante - CPF</span></th>
                    <th><span>Passagem</span></th>
                    <th><span>Status</span></th>
                </tr>
            </thead>

            <tbody>
                @foreach (Passagem participante in Model.ListaParticipante)
                {
                    <tr>
                        <td>
                            <input type="checkbox" id="@participante.Id" name="idsCredito" value="participante.Acao" @(participante.Acao == true || participante.Acao == false ? "checked = 'checked' readonly='readonly' " : string.Empty) />
                        </td>
                        <td>

                            <label id="@participante.Participante.Id" for="participanteId"> @(participante.Passagens != null ? participante.Participante.NomeCompleto + " - " + participante.Participante.CPF : "") </label>
                        </td>
                        <td>
                            <label id="@participante.Passagens" for="participantePassagem">@(participante.Passagens.ToString() != "" ? participante.Passagens.ToString() : "") </label>

                        </td>
                        <td>

                            <label id="@participante.Acao" for="participanteAcao">@(participante.Acao == true ? "Aprovado" : participante.Acao == false ? "Recusado" : "-")</label>

                        </td>
                    </tr>
                }
            </tbody>

        </table>
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />

        <input type="button" value="Aprovar" id="btAprovar" onclick="Aprovar(); return false;" onblur="Reload();" style="margin-left: 9px;" />

        <input type="button" value="Recusar" id="BtRecusar" onclick="Reprovar(); return false;" style="margin-left: 9px;" />

    </fieldset>
}

@section Scripts {
    @Scripts.Render("~/bundles/knockout")
    @Scripts.Render("~/bundles/jqueryui")
}
  • You’re sending as for view, nice post action and the view is easier to opine!

  • Just so you know IEnumerable<Periodo> is the collection responsible for the data including related?

  • 1

    Yes, this collection I have all the data I need to display on the screen...

  • So at View on @model put it like this: Put @model IEnumerable<Periodo> ... so you can access all items by this collection ...

  • When I did this, it returned errors in the properties saying that the methods were not defined, example: Model.periodo. You opened up properties for me to use for example, but you wouldn’t know how to do that... could you please help me

2 answers

1

Transform these methods in your Viewmodel into properties that are consuming the Ienumerable Period defined by the Controller

For example (Viewmodel class: Managepassagemmodel)

private IEnumerable<Periodo> listaPeriodo_
public IEnumerable<Periodo> ListaPeriodo { 
   get { return this.listaPeriodo_; } 
   set { this.listaPeriodo_ = value; } //Seta pelo Controller
}

public IEnumerable<int> ListaAno {
   get { 
       foreach (Periodo p in listaPeriodo_)
       {
           yield p.Ano;
       }
   }
}

public IEnumerable<Passagem> ListaParticipante {
   get { 
       foreach (Periodo p in listaPeriodo_)
       {
           for each (Passagem ps in p.Passagem)
               yield ps;
       }
   }
}

On your view, just use:

@foreach (Passagem participante in Model.ListaParticipante.Distinct())
@foreach (int ano in Model.ListaAno.Distinct().OrderBy(p => p))
@foreach (Periodo periodo in Model.ListaPeriodo)
  • Dude I understood what you did, my only problem is knowing which are the passages that were 2014 for example or 2013. I am not able to see in your code if this is possible, because I need to know who is who, because in my view when the user selects 2014, I need to bring everyone with the tickets 2014. Thank you

  • 1

    I think it is more advisable to create a function that does this in the class periodo that you use to bring this IEnumerable where this function will bring from Entity only the passages of a certain period.

  • @Sirsmart, Model.ListaParticipante.Distinct().Where(p.Ano = 2014) I believe this page needs to be prepared to order the server and bring the results by request. Otherwise I suggest you bring everything and search by javascript (more complex)

  • @Fulvius, he said he understood what I did. I removed my comments.

-1

Doesn’t seem to be a problem of View: is only a feature of the Entity Framework, which does not load the participants alone because it is a nesting level greater than 1, i.e., loads the passages (level 1), but does not load the aggregated data of passages (level 2 or more).

I don’t know what your code is on Controller, but would recommend using the .Include() to force the aggregate data load. For example:

var gerenciaPassagens = context.GerenciaPassagensModels
    .Include(gp => gp.Passagem.Participante)
    .Where(/* Seus parâmetros para carga das informações */)

return View(gerenciaPassagens);

Browser other questions tagged

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