Html.ActionLink (using sample from ScottGu's first MVC post).

Last post 12-11-2007 1:14 AM by tditiecher. 1 replies.

Sort Posts:

  • Html.ActionLink (using sample from ScottGu's first MVC post).

    12-10-2007, 6:12 PM
    • Member
      2 point Member
    • tditiecher
    • Member since 02-02-2007, 4:39 AM
    • Posts 3

    I tried to recreate the sample from ScottGu's first MVC post (http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx).

    When I define the Categories.aspx view as mentioned in the article, Html.ActionLink method generates the hyperlink /Products/List (omitting the category):
    <li><%= Html.ActionLink(category.CategoryName, new { action="List", category = category.CategoryName})%></li>

    But when I change this to the following, it works and the category name is added to the hyperlink:
    <li><%= Html.ActionLink(category.CategoryName, new { action="List", id = category.CategoryName})%></li>

    I didn't change the routing in the global.asax.cs. Can someone explain why the first code is not working as expected? 

    With kind regards,
    Taco Ditiecher. 

     

  • Re: Html.ActionLink (using sample from ScottGu's first MVC post).

    12-11-2007, 1:14 AM
    Answer
    • Member
      2 point Member
    • tditiecher
    • Member since 02-02-2007, 4:39 AM
    • Posts 3

    I found my mistake; I tried adding a new route to the routing table (in my global.asax.cs), but I placed it after the existing ones, instead of before.

    The following code did the trick: 

    protected void Application_Start(object sender, EventArgs e) {
    	RouteTable.Routes.Add(new Route {
    		Url = "Products/List/[category]",
    		Defaults = new { controller = "Products", action = "List", category = (string)null },
    		RouteHandler = typeof(MvcRouteHandler)
    	});
    	RouteTable.Routes.Add(new Route {
    		Url = "[controller]/[action]/[id]",
    		Defaults = new { action = "Index", id = (string)null },
    		RouteHandler = typeof(MvcRouteHandler)
    	});
    	RouteTable.Routes.Add(new Route {
    		Url = "Default.aspx",
    		Defaults = new { controller = "Home", action = "Index", id = (string)null },
    		RouteHandler = typeof(MvcRouteHandler)
    	});
    }
    
     

     

Page 1 of 1 (2 items)