views:

30

answers:

2

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);
}
A: 

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

Jason N. Gaylord
+2  A: 

This would be an HTML helper:

public static class HtmlExtensions
{
    public static MvcHtmlString RenderRecords(this HtmlHelper htmlHelper, IEnumerable<SiteContent> model)
    {
        // TODO: ...
    }
}

Which you call inside the view:

<%= Html.RenderRecords(Model) %>

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.

Darin Dimitrov
What assembly/directive does MvcHtmlString (or its successor, HtmlString) belong to? Also would the TODO be some LINQ and a recursive call to itself?
Don Boots
Reference: [MvcHtmlString](http://msdn.microsoft.com/en-us/library/system.web.mvc.mvchtmlstring.aspx). As far as the the TODO is concerned, as I've already mentioned in my answer checkout the linked [blog post](http://blogs.msdn.com/b/ericlippert/archive/2010/09/09/old-school-tree-display.aspx) which will be helpful in implementing it. I am sure you don't expect us writing the code in your place, so don't hesitate to ask if you have some specific problems implementing the proposed solution.
Darin Dimitrov