Grouping in foreach and view does not work

0

I created and set one variable to control the following. My LINQ returns me today 5 thousand lines. That variable, it's there to prevent it from repeating the same thing, like. Let's say that the linq returns 400 motif of ID = 1. Soon this variable, I the hedge so that only Motive 1 only appears on the screen once. Well, that worked. What's the problem then? For a single reason, I have 5 Business_Unit, for example. This BU can be repeated several times, as stated for the reason. I did similar to the Reason and it did not work.

1) The IF that I did, is printed on the page. 2) Even if I have more than one UN for each Reason, only one UN is shown and nothing more and so for the other levels below the UN. 3) I think the problem is with the Controller. Below is my controller (ActionResul) and my View (the part that mounts the TreeView.

Controller:

public ActionResult Acao()
        {
            RupturaEntities db = new RupturaEntities();

            var monta_arvore = db.Ruptura.Where(m => m.IDMotivo != 6)  
                               .Select(rup=>

                               new MontaArvoreAcao{
                                   IDRuptura = rup.IDRuptura,
                                   DataRuptura = rup.DataRuptura,
                                   IDMotivo = rup.IDMotivo,
                                   Motivo = rup.Motivo.Motivo1,
                                   IDOrigem = rup.IDOrigem,
                                   CodigoPDV = rup.CodigoPDV,
                                   UF  = rup.PDV.UF,
                                   Cidade = rup.PDV.Cidade,
                                   CnpjDescricao= rup.PDV.Cnpj + " - " + rup.PDV.Descricao,
                                   Codigo_Apresentacao = rup.Codigo_Apresentacao,
                                   Unidade_Negocio = rup.Apresentacao.Unidade_Negocio,
                                   Franquia = rup.Apresentacao.Franquia,
                                   Familia  = rup.Apresentacao.Familia,
                                   Descricao = rup.Apresentacao.Descricao
                               }).ToList().Take(50).OrderBy(r => r.IDMotivo);

            return View(monta_arvore.ToList());
        }

My View:

@{
                var _motivo = "";
                var _un = "";
                var _familia = "";
            <ul>
                @foreach (var item in Model)
                {
                    if (_motivo != @item.Motivo)
                    { 
                    <li item-checked='false' item-expanded='false'>
                        @item.Motivo
                        <ul>
                            <li item-checked='false' item-expanded='false'>
                                @item.Unidade_Negocio
                                <ul>
                                    <li item-checked='false' item-expanded='false'>
                                        @item.Familia
                                        <ul>
                                            <li item-checked='false' item-expanded='false'>
                                                @item.Descricao
                                            </li>
                                        </ul>
                                    </li>
                                </ul>

                            </li>
                        </ul>
                    </li>
                } 
                    _motivo = @item.Motivo;
                    _un = @item.Unidade_Negocio;
                    _familia = @item.Familia;
                }
            </ul>
           }

Personally, I put group by in LINQ and it did not work. It gives error. I tried to do it on the view and this has the problem of not recognizing some fields. In addition I did some IF's to control and the following happens. The first IF that is on the Reason works, that is, it does not allow the Reason to be repeated. The others do not work and are printed on the screen. See how my View was with these IF's.

<div id='jqxWidget'>
    <div style='float: left; width:auto;'>
        <div id='jqxTree' style='visibility: hidden; float: left; margin-left: 20px;'>

            @{
                var _motivo = "";
                var _un = "";
                var _familia = "";
            <ul>
                @foreach (var item in Model)
                {
                    if (_motivo != item.Motivo)
                    { 
                    <li item-checked='false' item-expanded='false'>
                        @item.Motivo
                        <ul>
                            if (_un != item.Unidade_Negocio)
                            { 
                            @foreach(var un in @item.Unidade_Negocio)
                            {
                            <li item-checked='false' item-expanded='false'>
                                @item.Unidade_Negocio
                                <ul>
                                    @foreach(var fam in @item.Familia)
                                    { 
                                    <li item-checked='false' item-expanded='false'>
                                        @item.Familia
                                        <ul>
                                            <li item-checked='false' item-expanded='false'>
                                                @item.Descricao
                                                <ul>
                                                    <li item-checked='false' item-expanded='false'>
                                                        @item.CnpjDescricao
                                                    </li>
                                                </ul>
                                            </li>
                                        </ul>
                                    </li>
                                    }
                                </ul>

                            </li>@*Unidade Negocio*@
                            }
                            }
                        </ul>
                    </li>
                } 
                    _motivo = @item.Motivo;
                    _un = @item.Unidade_Negocio;
                    _familia = @item.Familia;
                }
            </ul>
           }

        </div> 

Let me explain. In LINQ everything is loaded, as the template posted here.

Now, when I click on View, that's where some things happen. If I put a GroupBy in LINQ, according to example posted, it gives error in the first record, which in this case is the Reason, but could be any other. Error already posted here. The query brings me a lot of repetition, type, Reason, UN and etc ... Then I declare a variable and within the foreach, it will be set to the value it is controlling, type: Let's say Reason. It is empty value. Then I put an IF to know if the value of the variable is different from the reason. If it is (the first one always is) and then I load the Motive. If in the next iteration, it stays the same, there skips not repeating the same name. Well, since Motive is the first field, this is working well. When I get to the UN, it does not work there anymore. However, writing this post, I think I put the wrong place in the wrong place, that is, it should be at the end of the foreach of the UN and not in the foreach of the Reason, as it is. But I will test, but in any case, the other IFs are not working. The first works, but the others do not. That's what's messing up. I'm searching the net, but I have not seen anything really that solves my problem with nested IFs inside a View (CSHTML). This is my headache.

    
asked by anonymous 16.09.2014 / 21:32

1 answer

2

Have you tried GrupBy with Distinct when mounting the List?

Something like:

public ActionResult Acao()
    {
        RupturaEntities db = new RupturaEntities();

        var monta_arvore = db.Ruptura.Where(m => m.IDMotivo != 6)  
                           .Select(rup=>

                           new MontaArvoreAcao{
                               IDRuptura = rup.IDRuptura,
                               DataRuptura = rup.DataRuptura,
                               IDMotivo = rup.IDMotivo,
                               Motivo = rup.Motivo.Motivo1,
                               IDOrigem = rup.IDOrigem,
                               CodigoPDV = rup.CodigoPDV,
                               UF  = rup.PDV.UF,
                               Cidade = rup.PDV.Cidade,
                               CnpjDescricao= rup.PDV.Cnpj + " - " + rup.PDV.Descricao,
                               Codigo_Apresentacao = rup.Codigo_Apresentacao,
                               Unidade_Negocio = rup.Apresentacao.Unidade_Negocio,
                               Franquia = rup.Apresentacao.Franquia,
                               Familia  = rup.Apresentacao.Familia,
                               Descricao = rup.Apresentacao.Descricao
                           }).Distinct().ToList().Take(50).OrderBy(r => r.IDMotivo).ToList().GroupBy(x => x.IDMotivo).Select(x => x.First()).ToList();

        return View(monta_arvore.ToList());
    }
    
16.09.2014 / 22:00