Click here to Skip to main content
15,881,204 members
Articles / Web Development / ASP.NET

ASP.NET MVC: Part 2

Rate me:
Please Sign up or sign in to vote.
4.47/5 (6 votes)
5 Jul 2008CPOL7 min read 58.8K   1K   35  
A continued look at ASP.NET MVC and URL Routing.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using Mvc.Models;
using System.Web.Routing;

namespace Mvc.Views.Artists
{
    public partial class Artists :  ViewPage< List<Artist> > 
    {
        protected override void OnLoad(EventArgs e)
        {
            // Create the menu
            AddMenu();

            ArtistList.DataSource = ViewData.Model;
            // With the loosely coupled object collection, ViewData needs
            // to cast
            //ArtistList.DataSource = (List<Artist>)ViewData.Model;
            ArtistList.DataBind();
        }

        /// <summary>
        /// Create a simple alphabetic menu for navigation
        /// </summary>
        private void AddMenu()
        {
            // Build alphabetic menu
            for(char c = 'A'; c <= 'Z'; c++)
            {
                string link3 = Html.RouteLink(c.ToString(), new RouteValueDictionary(new { letter = c.ToString() }));
                string link2 = Html.ActionLink(c.ToString(), "ByName", "ByNameCotroller");
                //string link2 = Html.RouteLink(c.ToString(), "ArtistRoute", new RouteValueDictionary(new { controller = "Music", letter = c.ToString() }));

                string link = Html.ActionLink(c.ToString(), "Artists", new RouteValueDictionary( new { controller = "Music", id = c.ToString() }) );

                Menu.Controls.Add(new LiteralControl(link));
                // Add seperator
                if(c != 'Z')
                    Menu.Controls.Add(new LiteralControl(" | "));
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions