I'm trying to set up a dropdown menu that pulls from a Datatable. This works just fine for the first level of the menu.
Working code:
<ul class="dropdown-menu">
@foreach (System.Data.DataRow dr in menu.Rows)
{
if (Level1 != dr["Level1"].ToString())
{
<li><a href="#">@dr["Level1"].ToString()</a></li>
Level1 = @dr["Level1"].ToString();
}
}
</ul>
The problem occurs when I try to add a nested if statement. If you put this code into Visual Studio you will notice that the closing bracket for the @foreach
loop is not recognized by Razor.
Code breaks:
<ul class="dropdown-menu">
@foreach (System.Data.DataRow dr in menu.Rows)
{
if (Level1 != dr["Level1"].ToString())
{
<li><a href="#">@dr["Level1"].ToString()</a></li>
Level1 = @dr["Level1"].ToString();
if (Level2 != dr["Level2"].ToString())
{
<li><a href="#">@dr["Level2"].ToString()</a></li>
Level2 = @dr["Level2"].ToString();
}
}
} <!-- the issue is the bracket on this line -->
</ul>
You'll need to wrap the offending section in <text>
tags. See this answer: https://stackoverflow.com/a/6099659/1451531
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With