I have a feeling I'm doing this horribly, horribly wrong. Nested for loops? What is the best practice method of listing subcategories? I have a feeling it involves preparing the list in my controller action and sending it to the client via some actionresult, but I don't know where to start? Anybody able to point me in the right direction? Here's my hacky code:
 <h2>Categories</h2>
    <a href="javascript:;" onclick="newCategory()">Create New Category</a>
<br />
    <ul class="parent">
        <%foreach (var category in Model.Categories){%>
            <%-- List all of the top-level parent categories --%>
            <%if (category.IsParent && category.ParentId == 0)%>
            <li>
                <span class="buttons"><a href="javascript:;" onclick="editCategory(<%:category.CategoryId%>)" class="edit"></a> <a href="javascript:;" onclick="deleteCategory(<%:category.CategoryId%>)" class="delete"></a></span>
                <span class="categoryName"><%:category.CategoryName%></span>
                <span class="positionButtons"><%:Html.ActionLink(" ", "MoveCategoryUp", new {id = category.CategoryId},
                                                new {Class = "moveUp"})%><%:Html.ActionLink(" ", "MoveCategoryDown", new {id = category.CategoryId},
                                                new {Class = "moveDown"})%></span>
                <%-- List all of the subs for each parent --%>
                    <ul>
<%-- Level 1 --%>       <%foreach (var sub1 in Model.Categories){%>
                            <%if (sub1.ParentId == category.CategoryId){%>
                                <li>
                                    <span class="buttons"><a href="javascript:;" onclick="editCategory(<%:sub1.CategoryId%>)" class="edit"></a> <a href="javascript:;" onclick="deleteCategory(<%:sub1.CategoryId%>)" class="delete"></a></span>
                                    <span class="categoryName"><%:category.CategoryName%></span>
                                    <span class="positionButtons"><%:Html.ActionLink(" ", "MoveCategoryUp", new {id = sub1.CategoryId},new {Class = "moveUp"})%><%:Html.ActionLink(" ", "MoveCategoryDown", new {id = sub1.CategoryId},new {Class = "moveDown"})%></span>
                                    <%-- List all of the subs for each parent --%>
                                    <%if (sub1.IsParent){%>
                                    <ul>
<%-- Level 2 --%>                       <%foreach (var sub2 in Model.Categories){%>
                                            <%if (sub2.ParentId == sub1.CategoryId){%>
                                                <li>
                                                    <span class="buttons"><a href="javascript:;" onclick="editCategory(<%:sub2.CategoryId%>)" class="edit"></a> <a href="javascript:;" onclick="deleteCategory(<%:sub2.CategoryId%>)" class="delete"></a></span>
                                                    <span class="categoryName"><%:category.CategoryName%></span>
                                                    <span class="positionButtons"><%:Html.ActionLink(" ", "MoveCategoryUp", new {id = sub2.CategoryId},new {Class = "moveUp"})%><%:Html.ActionLink(" ", "MoveCategoryDown", new {id = sub2.CategoryId},new {Class = "moveDown"})%></span>
                                                    <%-- List all of the subs for each parent --%>
                                                    <%if (sub2.IsParent){%>
                                                    <ul>
<%-- Level 3 --%>                                       <%foreach (var sub3 in Model.Categories){%>
                                                            <%if (sub3.ParentId == sub2.CategoryId){%>
                                                                <li>
                                                                    <span class="buttons"><a href="javascript:;" onclick="editCategory(<%:sub3.CategoryId%>)" class="edit"></a> <a href="javascript:;" onclick="deleteCategory(<%:sub3.CategoryId%>)" class="delete"></a></span>
                                                                    <span class="categoryName"><%:category.CategoryName%></span>
                                                                    <span class="positionButtons"><%:Html.ActionLink(" ", "MoveCategoryUp",new {id = sub3.CategoryId},new {Class = "moveUp"})%><%:Html.ActionLink(" ", "MoveCategoryDown",new {id = sub3.CategoryId},new {Class = "moveDown"})%></span>
                                                                     <%-- List all of the subs for each parent --%>
                                                                    <%if (sub3.IsParent){%>
                                                                    <ul>
<%-- Level 4 --%>                                                       <%foreach (var sub4 in Model.Categories){%>
                                                                            <%if (sub4.ParentId == sub3.CategoryId){%>
                                                                                <li>
                                                                                    <span class="buttons"><a href="javascript:;" onclick="editCategory(<%:sub4.CategoryId%>)" class="edit"></a> <a href="javascript:;" onclick="deleteCategory(<%:sub4.CategoryId%>)" class="delete"></a></span>
                                                                                    <span class="categoryName"><%:category.CategoryName%></span>
                                                                                    <span class="positionButtons"><%:Html.ActionLink(" ", "MoveCategoryUp", new {id = sub4.CategoryId}, new {Class = "moveUp"})%><%:Html.ActionLink(" ", "MoveCategoryDown", new {id = sub4.CategoryId}, new {Class = "moveDown"})%></span>
                                                                                    <%-- If more than 4 levels of subcategories are required, put another level here --%>
                                                                                </li>
                                                                            <%}%>
                                                                        <%}%>
                                                                    </ul>
                                                                    <%}%>
                                                                </li>
                                                            <%}%>
                                                        <%}%>
                                                    </ul>
                                                    <%}%>
                                                </li>
                                            <%}%>
                                        <%}%>
                                    </ul>
                                    <%}%>
                                </li>
                            <%}%>
                        <%}%>
                    </ul>
            </li>
        <%}%>
    </ul>
Edit
Unfortunately this code isn't rendering the results I'm looking for, so I cant really provide much more than this: http://jsfiddle.net/EeaGr/ each list item has buttons for edit/delete and moveup/movedown options for its category. My category has the following properties:
CategoryID : int
Name: string
ParentID : int
IsParent : bool
Position: int