我有一種感覺,我正在做這個可怕的,可怕的錯誤。嵌套循環?列出子類別的最佳做法是什麼?我有一種感覺,它涉及在我的控制器動作中準備列表,並通過一些動作結果發送給客戶端,但我不知道從哪裏開始?任何人都能指出我正確的方向?這裏是我的哈克代碼:使用asp.net生成類別和子類別列表mvc2
<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:整數
名稱:字符串
PARENTID:整數
IsParent:布爾
位置:整數
上帝的聖母。 – 2010-09-24 21:11:35
我有點難以想象這個輸出!有沒有可能在http://jsfiddle.net/中提供HTML的渲染視圖?你也可以添加CSS,所以我們可以看看你在頁面上看到的內容 – DaveDev 2010-09-24 21:29:40
哈哈,2年後,我在看這個,我只能重複達林提到的......「上帝的聖母」 ...我在想什麼? – Gallen 2013-01-03 20:25:27