Nest tag ul in another tag ul with foreach

0

I have this foreach, which I did with your help here at SOPT. I made it work, but it worked. Only I did it with just one knot. What I'm going to need is this. I have a parent node, inside that parent node a child, inside the child a grandson and so on. My model returns me all the required fields. I have tried to do it in several ways and all give error in the tag because it says the message that it is not possible to nest tags the way I am doing. See, this code below, I can bring all the reasons.

<ul>
   @foreach (var item in Model)
      {

         <li item-checked='false' item-expanded='false'>
              @item.Motivo
         </li>
       }
</ul>

Now I need to bring in that motive, the business units. These values or data comes from the same Model, it would look something like this:

<ul>
    <li item-checked='false' item-expanded='false'>
       @item.Unidade_Negocio
    </li>
</ul>

However, if I put this code inside the main foreach it gives error in the <ul> tag. But for my little experience, I'll need a <ul> tag to go to positioning the Business_Use, and more, I need, in my understanding of another foreach only for Business_Unit. Then I can not walk anymore. This is because I could not make it work via jquery , because my jquery already has everything in each , but I could not mount the check's and so I'm working directly on cshtml(view) .

I did as the colleague Miguel suggested, but it gives an error:

<ul>
                @foreach (var item in Model)
                {

                    <li item-checked='false' item-expanded='false'>
                        @item.Motivo
                    </li>
                    <li>
                        <ul>
                            @foreach (var negocio in Model.Unidade_Negocio)
                            {
                                <li item-checked='false' item-expanded='false'>
                                    @negocio.Unidade_Negocio
                                </li>
                            }
                        </ul>
                    </li>
                }
            </ul>

The error you give is this:

Additional information: 'object' não contém uma definição para 'Unidade_Negocio'

This is my Action with linq. It's a general linq.

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);

            return View(monta_arvore);
        }

So you are now:

<ul>
                        @foreach (var item in Model)
                        {

                            <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>
                        }
                    </ul>
    
asked by anonymous 16.09.2014 / 15:29

1 answer

2

It is possible to give a tag error, because to put a ul inside others, you have to put it inside a li , getting something like:

<ul>
   @foreach (var item in Model)
      {
         <li item-checked='false' item-expanded='false'>
              @item.Motivo
         </li>
         <li>
            <ul>
                foreach (var item2 in item.Unidade_Negocio){//aqui colocas a lista que queres percorrer, item.Unidade_Negocio a partida só terá um elemento e não dá para fazer um foreach
                    <li item-checked='false' item-expanded='false'>
                       @item2.Unidade_Negocio
                    </li>
                }
            </ul>
         </li>
       }
</ul>
    
16.09.2014 / 15:39