.NET/MVC 递归记录列表
我正在尝试通过以下方式列出内容来使我的内容 CMS 更加用户友好
: - 子页面 - - 子页面 - - - 子页面 - - - - 等等...
使用.NET/MVC2,这个函数将在哪里定义以及如何调用它。
这是我列出我的内容的页面:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Content.master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<asp:Content ID="Head" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<ul>
<%
foreach (var item in Model) {
string contentTitle = item.Title;
%>
<li class="row"><%: Html.ActionLink(contentTitle, "contentedit", new { id = item.ID }) %></li>
<!-- List subpages recursively -->
<% } %>
</ul>
</asp:Content>
这是我在控制器中的操作:
public ActionResult Content()
{
// Get just parent items -- for now.
List<SiteContent> viewData = DB.SiteContents.Where(c => c.ParentID == null).OrderBy(c => c.ParentID).ToList();
return View(viewData);
}
I'm trying to make my content CMS more user friendly by listing content in the following fashion:
Parent
- Sub Page
- - Sub Page
- - - Sub Page
- - - - etc...
Using .NET/MVC2, where would this function be defined and how would it be called.
This is my page listing my content:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Content.master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<asp:Content ID="Head" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<ul>
<%
foreach (var item in Model) {
string contentTitle = item.Title;
%>
<li class="row"><%: Html.ActionLink(contentTitle, "contentedit", new { id = item.ID }) %></li>
<!-- List subpages recursively -->
<% } %>
</ul>
</asp:Content>
This is my Action in my Controller:
public ActionResult Content()
{
// Get just parent items -- for now.
List<SiteContent> viewData = DB.SiteContents.Where(c => c.ParentID == null).OrderBy(c => c.ParentID).ToList();
return View(viewData);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将是一个 HTML 帮助器:
您可以在视图中调用它:
就实现而言,您可以查看 Eric Lippert 的博客,他最近 写了一篇关于转储递归树老派的文章。您所需要做的就是用适当的 html 标签(
ul
、li
)替换 ASCII 符号。另外使用 TagBuilder 也是一个不错的选择想法而不是硬编码 html 标签。This would be an HTML helper:
Which you call inside the view:
As far as the implementation is concerned you may take a look at Eric Lippert's blog who recently wrote an article about dumping a recursive tree old school. All you need is to replace the ASCII symbols with appropriate html tags (
ul
,li
). Also using a TagBuilder would be a good idea instead of hardcoding html tags.如果您只是尝试通过视图传回数据来列出数据,则可以执行如下操作:
http://www.asp.net/mvc/tutorials/displaying-a-table-of-database-data-cs
If you are simply trying to list data by passing the data back through the View, you can perform something like this:
http://www.asp.net/mvc/tutorials/displaying-a-table-of-database-data-cs