Nest <ul> tag in another <ul> tag with foreach

Asked

Viewed 210 times

0

I have this foreach, which I did with your help here at SOPT. I cost to make it work, but it worked. Only I did it with just one knot. What I’m going to need is this. I have a father knot, inside that father knot a son, inside the son a grandson and so on. My model returns me all the required fields. I have tried to do it in several ways and they all make a mistake in the tag, because it says the message that it is not possible to nest tags the way I am doing. Look at this, 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 this reason, the trade units_business. These values or data come from the same Model, would be more or less like this:

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

However, if I put this code inside the foreach main gives tag error <ul>. But from my little experience, I’m gonna need a tag <ul> to position Unidade_negocio, and more, precise, to my understanding of another foreach only for Unidade_negocio. Then I can’t walk anymore. This whole job is because I couldn’t make it work jquery, because by my jquery already brings everything in each, but I couldn’t assemble the checks and so I’m working directly on cshtml(view).

I did so as Miguel suggested, but it is a mistake:

<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 mistake is this:

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

This is my Action with the Bible. It’s a general Action.

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

That’s the way it is 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>
  • Don’t change the question after you get an answer. So you invalidate mine and it gets confusing for someone who comes to read the question for the first time, changing the initial problem you had

  • This change that changes the meaning of the question. First the error was of the ul, now it is error to go through the foreach

1 answer

2


It is possible that it will give a tag error, by 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>
  • Yeah, when I put the Model.Unidade_Negocio was to set an example... You know what you have to put in the foreach. Maybe it is even item.Unidade_Negocio. But don’t forget it has to be a list

  • @pnet, you always have to have a list to go through when you do a foreach. If you want to create a data list Unidade_Negocio you already have to pass the controller that list.

  • In the controller the variable is of the type List, because in the Linq I return a Tolist() in it.

  • Yes, but it has nothing to do with that. For example, you are returning a list, which will be your Model. Now to do another foreach you have to pass another list, in a viewbag for example. Put here the fields of your Model sff

  • Let me understand. I’ll have to do another LINQ to return the unit? Is that it? And if it is, should I do the same for Family and Product? That’s it?

  • That’s right. Ask the question what you’re doing and the data from your table sff

  • Just tell me something, is your Business Unit a data list?? Try to give more details of how you have your tables.

  • It is inside the main Lake. It is a field that the Lake brings along with other fields.

  • It means that a Rupture has several business units, right?

  • That’s right. So: One Reason => Several UN. One UN => Several Families. One Family => Several Products.

  • And what is the UN id that connects with Motive? The Reason is Rupture?

  • I’m coming to the conclusion that LINQ is totally wrong. I can’t bring everything back the way it is. I made a form that apparently worked, but only comes: A REASON and this one has a UN and this one has a FAMILY and this one has a PRODUCT. This is coming in the wrong way, due to the way with the LINQ is. I’ll fix it. I will edit and post how I managed to get colleagues to review whether it is correct or not.

  • Okay, that’s where I wanted to get to. You’re going to have to have two lists, one for Motive and one for UN. Then from there you will buy the id’s to build the view

Show 8 more comments

Browser other questions tagged

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