Nest tag in another tag with foreach
I have this foreach, which I did with the help of you here at SOPT. It cost me to make it work, but it worked. Only I did it with only one knot. What I'm gonna need is this. I have a father node, within that Father node a son, within the son a grandson and so it goes. My model returns all the necessary fields. I have tried to do several ways and they 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 within this reason, the business units. These values or data comes from the same Model, it 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 main foreach
it gives error in the tag <ul>
. But for my little experience, I will need a tag <ul>
to go positioning the Unit_negocio, and more, I need, in my understanding of another foreach only for the Unit_negocio. There I do not I can walk more. This whole trap is because I could not make it work via jquery
, because for my jquery
already brings everything in each
, but I could not mount the check's and so I am working directly in cshtml(view)
.
I did so as colleague Miguel suggested, but gives 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 it gives 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 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>
1 answers
It is possible that it gives 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>