Unlimited multi level menu in mvc
<p>I have created multilevel menu in mvc. it works but i want to make count of level unlimited. There are 4 levels right now so in the future if someone wants to add 5. level in the database, it will not show up in my menu. I need to add another foreach loop to display 5. level. So how can i make my menu unlimited level?</p>

<p>here is my menu view.</p>

<pre><code> @{       
    var myMenu = @Model;
    var navbarmenu = myMenu.Where(x =&gt; x.ParentID == null);
    var i = 0;
    &lt;nav class="navbar navbar-default"&gt;
        &lt;div class="container-fluid"&gt;
            &lt;div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"&gt;
                &lt;ul class=" nav navbar-nav"&gt;
                    @foreach (var menuLevel1 in navbarmenu)
                    {
                        &lt;li class="dropdown"&gt;
                            &lt;a href="~/ADDS/Default.aspx" style="font-size:16px;"&gt;
                                @menuLevel1.MenuName
                                @if (menuLevel1.MenuName != "Home")
                                {
                                    &lt;span class="caret"&gt;&lt;/span&gt;
                                }
                            &lt;/a&gt;
                            @{ var navbarsubmenu = myMenu.Where(x =&gt; x.ParentID == menuLevel1.MenuID);}
                            @if (navbarsubmenu.IsAny())
                            {
                                &lt;ul class="dropdown-menu"&gt;
                                    @foreach (var menuLevel2 in navbarsubmenu)
                                    {
                                        &lt;li class="dropdown-submenu"&gt;
                                            @if (!string.IsNullOrEmpty(menuLevel2.MenuLink))
                                            {
                                                &lt;a href="@Url.Content(menuLevel2.MenuLink)"&gt;
                                                    @menuLevel2.MenuName
                                                &lt;/a&gt;
                                            }
                                            else
                                            {
                                                &lt;a href="~/ADDS/Default.aspx"&gt;
                                                    @menuLevel2.MenuName
                                                &lt;/a&gt;
                                            }
                                            @{var navbarsubmenu2 = myMenu.Where(x =&gt; x.ParentID == menuLevel2.MenuID);}
                                            @if (navbarsubmenu2.IsAny())
                                            {
                                                &lt;ul class="dropdown-menu"&gt;
                                                    @foreach (var menuLevel3 in navbarsubmenu2)
                                                    {
                                                        &lt;li class="dropdown-submenu"&gt;
                                                            @if (!string.IsNullOrEmpty(menuLevel3.MenuLink))
                                                            {
                                                                &lt;a href="@Url.Content(menuLevel3.MenuLink)"&gt;
                                                                    @menuLevel3.MenuName
                                                                &lt;/a&gt;
                                                            }
                                                            else
                                                            {
                                                                &lt;a href="~/ADDS/Default.aspx"&gt;
                                                                    @menuLevel3.MenuName
                                                                &lt;/a&gt;
                                                            }
                                                            @{var navbarsubmenu3 = myMenu.Where(x =&gt; x.ParentID == menuLevel3.MenuID);}
                                                            @if (navbarsubmenu3.IsAny())
                                                            {
                                                                &lt;ul class="dropdown-menu"&gt;
                                                                    @foreach (var menuLevel4 in navbarsubmenu3)
                                                                    {
                                                                        &lt;li class="dropdown-submenu"&gt;
                                                                            @if (!string.IsNullOrEmpty(menuLevel4.MenuLink))
                                                                            {
                                                                                &lt;a href="@Url.Content(menuLevel4.MenuLink)"&gt;
                                                                                    @menuLevel4.MenuName
                                                                                &lt;/a&gt;
                                                                            }
                                                                            else
                                                                            {
                                                                                &lt;a href="~/ADDS/Default.aspx"&gt;
                                                                                    @menuLevel4.MenuName
                                                                                &lt;/a&gt;
                                                                            }
                                                                        &lt;/li&gt;
                                                                    }
                                                                &lt;/ul&gt;
                                                            }
                                                        &lt;/li&gt;
                                                    }
                                                &lt;/ul&gt;
                                            }
                                        &lt;/li&gt;
                                    }
                                &lt;/ul&gt;
                            }
                        &lt;/li&gt;
                    }
                &lt;/ul&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/nav&gt;
}
</code></pre>