Result of foreach behaves unexpectedly

-1

I've been working on a page, where I need to create a treeview system with checkbox and this is consuming a lot of my time and leaves me worried about the project SLA. In the controller, there I have a LINQ that returns everything I need on the page. But I think the way it is, may be affecting View's behavior, but I'm not sure. Action returns me several vampos. There is an attribute there that is the IDRupture and because of it, I have several records that are the same. Well, I tried to solve in their view their behavior, that is, that each node only repeated the name once. From then on my problem starts. I will try to explain to you and see if I can get a definite help. If this is resolved, the part of the screen with the treeview is resolved. The first node is the Reason. The way I did on the page (view) is working correctly. Within the Reason, there I have another node, called Business_Unit. From there the zica begins. For example, for the Reason="I SELLED ALL STOCK" I have a total of 865 records and 3 Business_Units (DERMOCOSMETICS, GENERICS, and MIP). But when I mount my Foreach, it only shows MIP only once. One time is correct, but it should show the others too. Then I thought it might be for want of ordination. I went to sort several fields and gave me an error. I have ordered several fields like this: .OrderBy (r => new {r.campo1, r.campo2 ..}). What I need is the following. I bring my first motive. When I click on the little box next to the checkbox (expand) you should have 1 or more nodes with the Business_Usage. Expanding each of the UN there should appear all the families within that UN, which can be 1 or more and so on. I stumbled by posting, respectively: The OrderBy, Controller and View Error, in that order. Error:

Controller:

publicActionResultAcao(){RupturaEntitiesdb=newRupturaEntities();varmonta_arvore=db.Ruptura.Where(m=>m.IDMotivo!=7).Select(rup=>newMontaArvoreAcao{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,
                                   Codigo_Unidade_Negocio = rup.Apresentacao.Codigo_Unidade_Negocio,
                                   Franquia = rup.Apresentacao.Franquia,
                                   Familia = rup.Apresentacao.Familia,
                                   Descricao = rup.Apresentacao.Descricao

                               }).ToList().OrderBy(r => new { r.IDMotivo, r.Codigo_Unidade_Negocio, r.Familia, r.Descricao });==>> ***Aqui dá erro. Se remover o new, funciona.***

            return View(monta_arvore.ToList());
        }

View:

<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 = "";
                var _desc = "";
                var _apr = "";
            <ul>
                @foreach (var item in Model)
                {
                    if (_motivo != @item.Motivo)
                    { 
                    <li item-checked='false' item-expanded='false'>
                        @item.Motivo
                        <ul>

                            @foreach(var un in @item.Unidade_Negocio)
                            {
                                if(_un != @item.Unidade_Negocio)
                            {
                            <li item-checked='false' item-expanded='false'>
                                @item.Unidade_Negocio
                                <ul>

                                    @foreach(var fam in @item.Familia)
                                    { 
                                     if(_familia != @item.Familia)
                                     {
                                    <li item-checked='false' item-expanded='false'>
                                        @item.Familia
                                        <ul>
                                        @foreach(var desc in @item.Descricao)
                                         {
                                           if(_desc != @item.Descricao)
                                           {
                                            <li item-checked='false' item-expanded='false'>
                                                @item.Descricao
                                                <ul>
                                                    @foreach (var apr in @item.CnpjDescricao)
                                                    {
                                                    <li item-checked='false' item-expanded='false'>
                                                        @item.CnpjDescricao
                                                    </li>
                                                    }
                                                </ul>
                                            </li>
                                               _desc = @item.Descricao;
                                        }
                                        }
                                        </ul>
                                    </li>
                                        _familia = @item.Familia;
                                    }
                                    }
                                </ul>

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

                    </li>
                } 
                    _motivo = @item.Motivo;
                }
            </ul>
           }

        </div>

        <div style='margin-left: 60px; float: left;'>
            <div style='margin-top: 10px;'>
                <input id='jqxCheckBox' type="hidden">
            </div>
        </div>

    </div>
</div>

I would avoid this bunch of nested Foreach and IF's, but I do not see how. Any help is great. This is not a broad question. The foreach, may also be the cause of the situation in which I pass.

    
asked by anonymous 18.09.2014 / 15:45

1 answer

1

Friend, it is the way the objects are structured, these FOREACHs and IFs will be difficult to file keeping the structure of objects, what you can do to make it a little more organized is to already put the IF inside the FOREACH, since the counter condition of the IF is never used, as:

@foreach (var item in Model.Where(x => x.Motivo != _motivo))
    
29.09.2014 / 20:25