[RESOLVED]Menu Navigation with SiteMaps

Hello…

I read the tutorial "Providing Website Navigation with SiteMaps" (http://www.asp.net/learn/mvc/tutorial-20-vb.aspx) and I think it is an example more for Custom Helpers than a Menu implementation.

It proposes hard-code all the structure of the menu. What if I want to add classs or if I want add an special item?

For my menu I use a User Control in my Site.Master.

This is te code of the User Control:

                <div id="nav">
                   
            <asp:Repeater runat="server" ID="siteMapAsBulletedList" DataSourceID="SiteMapDS">
                <HeaderTemplate>
                    <ul>
                        <li class="<%# SiteMap.RootNode["class"] %>"><a href="<%# SiteMap.RootNode.Url %>" title="<%# SiteMap.RootNode.Description  %>"><%# SiteMap.RootNode.Title %></a></li>
                </HeaderTemplate>
               
                <ItemTemplate>
                        <li class="<%# Eval("[class]") %>"><a href="<%# Eval("Url") %>" title="<%# Eval("description") %>"><%# Eval("title") %></a></li>
                </ItemTemplate>
               
                <FooterTemplate>
                    </ul>
                </FooterTemplate>
            </asp:Repeater>
            <asp:SiteMapDataSource ShowStartingNode="false" ID="SiteMapDS" runat="server" />
               
                </div>
                

And, at moment, it´s working. And no code behind or helpers. No compilation if some changes.

Well… As I´m not an expert, can you tell me if, in my new MVC project, it´s a good practice or I should use a custom helper.

Thanks
 

Hey,

In general you should avoid using runat="server" controls in MVC project. Of course they will usually work, unless you try to use features that depend on viewstate or postbacks. Still for pure rendering bases they can do, yet you really should depend on
helper extensions, in this case the helper extension methods would have to be provided by you and directly deal with html output rendering.

Maciej

+1 for not using web server controls (though we really need a mark up model for v2.0 of MVC)

 and +1 for not using sitemaps for anything but demo apps :)

Luis Abreu

+1 for not using web server controls (though we really need a mark up model for v2.0 of MVC)

 

Seeing contents of RC futures assembly, seems like its coming!

The namespace Microsoft.Web.Mvc.Controls contains some basic runat server MVC friendly controls, including form input controls and THE repeater. All of those are ViewData friendly [:D]

Maciej

I’ve spotted them though I still haven’t had a good look at them…

Thanks everybody..

But Luis.. then the recomendation is:

- For the Menu use a Custom helper (as tutorial does)?

- Don’t use SiteMaps… But it’s incredible… I’m working a site for production and I use for all navigation the SiteMap.. What are the problems with it?

Thanks!

 

Well…

I delete the Repeater and create a new navigation.

1. Create a base controller, so all my controllers have a common Menu:

    public class ApplicationController : Controller
    {

            #region ApplicationController
            public ApplicationController ()
            {
                ViewData ["DataSiteMap"] = dataSiteMap () ;
            }
            #endregion

   And I created a method (dataSiteMap) that returns a DataTable with all the info that my views need to construct the menu: name, description, url and class.

 

2. Put all my controllers to inherit from this base class:

public class HomeController : ApplicationController

 

3.  I create a ViewUserControl (in the VIEWS SHARED folder) with this MVC friendly code (hehehe.. no more repeater):

Code of  navigation.ascx:

<%@ Import Namespace="System.Data" %>
<div id="nav">
    <ul>
    <%  foreach ( DataRow itemMenu in (ViewData ["DataSiteMap"] as DataTable).Rows  )  {  %>
        <li class="<%= itemMenu["class"] %>"><a href="<%= itemMenu["url"] %>" title="<%= itemMenu["description"] %>"><%= itemMenu["name"] %></a></li>
    <%  }  %>
    <ul>
</div>

 

4. Finally I load the ViewUserControl in my Site.Master:

        <% Html.RenderPartial ("navigation" ) ; %>

 

Now:

- All my pages has the same menu.

- I don´t use a Repeater with Runat Server.

- And I have a menu with a easy to change HTML render.

 

Well.. What do you think? A good solution?

Leave a Reply