Foreach result behaves unexpected

Asked

Viewed 104 times

-1

I have 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’s SLA. In the controller, there I have a LINQ that returns me everything I need on the page. But I think the way it is, it may be affecting the behavior of the View, but I’m not sure. Action returns me several vampos. There’s an attribute there that’s Idruptura, and because of him, I have a lot of identical records. Well, then I tried to solve in the view itself their behavior, that is, that each node repeated only once the name. From now on my problem begins. I will try to explain to you and see if I can get a definitive help. If you solve this, the part of the screen with treeview is solved. The first node is Reason. The way I did on the page(view) is working correctly. Inside the Motif, then I have another node, called Unidade_negocio. From there the Zica begins. For example, for the Reason = "SOLD ALL STOCK" I have a total of 865 records and 3 business units (DERMOCOSMETICOS, GENERICS and MIP). However when I mount my Foreach, it only shows MIP once. Once is correct, but should show the others too. Then I thought it might be for lack of ordination. I went to order several fields and gave me an error. I ordered several fields like this: . Orderby(r => new {r.campo1,r.campo2..}). What I need is the following. I bring my First Reason. When I click on the arrow next to the checkbox(expand) you should have 1 or more nodes with the Business Unit. I expand each of the UN there should appear all the families within that UN, which can be 1 or more and so on. He was posting, respectively: The Orderby Error, Controller and View, in that order. Error:

inserir a descrição da imagem aqui

Controller:

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

            var monta_arvore = db.Ruptura
                               .Where(m => m.IDMotivo != 7)

                               .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,
                                   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 like to avoid this bunch of nested Foreach and IF’s, but I don’t see how. Any help is great. That’s not a broad question. The foreach may also be the cause of the situation in which I pass.

  • I’m sure the problem is logical, but I can’t see anything other than the one I did.

  • Shows the result of this screen... a print!

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 leave a little more organized is to already put the IF inside the FOREACH, since the contrary condition of the IF is never used, as:

@foreach (var item in Model.Where(x => x.Motivo != _motivo))

Browser other questions tagged

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