Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HTML
@if (ViewBag.categories != null)
   {        <ul id="category">
      @foreach (var Categories in @ViewBag.categories)
       {   if(@Categories.ParentID==null)
            {    <li id="categoryli"> @Categories.Name 
                      <ul id="subcategory" class="submenu"> 
                         @foreach(var ParentId in @ViewBag.categories)
                         {
                               if( @Categories.CategoryID==@ParentId.ParentID)
                                 {
                                  <li>
                                   @ParentId.Name
                                   </li>
                                  }}
                </ul>          </li> 
             }        }
            </ul>
    }




I have written my jquery like this



JavaScript
$(document).ready(function () {
        
        $('#category >li').click(function () {
            debugger;
            if ($('#category >li > #subcategory').hasClass('submenu')) {
                //$(this).removeClass("submenu");
                $('ul#category >li > #subcategory').slideToggle('slow');
            }
                   });
    });




css


CSS
.submenu {
    display:none;
}



while clicking on particular li all nested ul elements are toggling.
Posted
Updated 13-Nov-14 23:01pm
v2

1 solution

First things first, if your already in a code block, you do not want @ toggles except when directly rendering to the browser.

Secondly, you're calling a foreach loop on precisely the same collection in your submenu. That's likely not what you meant to do.

You also want to apply an id as a unique element in HTML, you're using it as if it were a class, multiple instances of the same id in the same document: this will break jQuery. Better to apply classes to your tags instead.

If you have a collection of parent and children nodes just thrown together as well, that's not the best data access scheme. Anyway, here's a corrected version of your Razor, which should do what it looks like you're trying to do:

C#
@if (ViewBag.categories != null)
{        <ul id="category">
        @foreach (var category in ViewBag.categories)
        {
            if (category.ParentID == null)
            {
            <li class="categoryli">
                @category.Name
                <ul class="submenu">
                    @foreach (var subcategory in category.categories)
                    {
                        if (category.CategoryID == subcategory.ParentID)
                        {
                            <li>
                                @subcategory.Name
                            </li>
                        }
                    }
                </ul>
            </li>
            }
        }
    </ul>
}


If ParentID is an it you'll need to do if(category.ParentID == 0)

Now you'll need to modify your jQuery:

JavaScript
$(function () { 
    $('.submenu').hide();       
    $('.category >li').click(function () {
        $(this).children('.submenu').slideToggle('slow');
    });
});


And finally, I would remove the submenu css, or at least remove the display:none; from it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900