代码之家  ›  专栏  ›  技术社区  ›  Grahame A

生成类别和子类别列表asp.netmvc2型

  •  2
  • Grahame A  · 技术社区  · 14 年前

    我有一种感觉,我在做这个可怕的,可怕的错误。嵌套for循环?列出子类别的最佳实践方法是什么?我有一种感觉,它包括在我的控制器操作中准备列表,并通过一些actionresult发送给客户机,但我不知道从哪里开始?有人能给我指出正确的方向吗?这是我的黑客代码:

     <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>
    

    编辑

    不幸的是,这段代码没有呈现我想要的结果,所以我不能提供更多: http://jsfiddle.net/EeaGr/ 每个列表项都有编辑/删除按钮和其类别的上移/下移选项。我的类别具有以下属性:

    类别ID:内部

    名称:字符串

    我的父母:布尔

    职位:内景

    1 回复  |  直到 14 年前
        1
  •  6
  •   svick bala    14 年前

    首先,我会改变类别的结构,使每个类别 Category 有一个 Subcategories 财产。

    类别 如果这个类别有子类别,它会递归地调用自己:

    类别控制.ascx

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Category>" %>
    <%@ Import Namespace="so_subcats.Model" %>
    <li><%= Model.Name %>
        <% if (Model.Subcategories != null) { %>
            <ul>
            <% foreach (Category subcat in Model.Subcategories)
                 Html.RenderPartial("CategoryControl", subcat); %>
            </ul>
        <% } %>
    </li>
    

    然后只需创建一个视图,为每个顶级类别呈现此控件:

    类别.aspx :

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
      Inherits="System.Web.Mvc.ViewPage<IEnumerable<Category>>" %>
    <%@ Import Namespace="so_subcats.Model" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
        Categories
    </asp:Content>
    
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
        <h2>Categories</h2>
            <ul>
            <% foreach (Category cat in Model)
                 Html.RenderPartial("CategoryControl", cat); %>
            </ul>
    
    </asp:Content>
    

    当然,如果不想改变类的结构,也可以使用这个解决方案,只需稍微修改一下即可。