views:

1041

answers:

3

I have urls that look like this

~\articles\energy\topweek

~\articles\metals\latestpopular

where second url string is a category and third is a filter

so route looks like this

    routes.MapRoute("ArticleFilter",
     "articles/{category}/{filter}",
  new { controller="Article", action="Filter" })

That's pretty easy and everything works fine.

So lets say if i'm looking at articles{category}\ default view.

How do I construct links to point to current category with filters.

Example: if current page articles\energy

I need to construct article\energy\topweek and article\energy\latestpopular.

Where category should be dynamic based on the current page. preferably in a partial view so I can use it across different pages.

Thank you

+1  A: 

Use the UrlHelper class to construct the route url's, after splitting the url strings to get the information you need.

string url = "~\articles\film\topweek";
string[] parts = url.Split("\\");
string cat = parts[2];
string fil = parts[3];

string actionUrl = UrlHelper.RouteUrl("ActionFilter", new { category = cat, filter = fil });
Tomas Lycken
A: 

You have to use Html.RouteLink (or Html.ActionLink) to build the URL.

<% Html.RouteLink('link text', routeName, new { filter = "topweek" }) %>
Drevak
+1  A: 

Create an object for your usercontrol to take as model like this :

public class ArticleLinksControl {
    public string CategoryName { get; set; }
}

And your user control : <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Namespace.ArticleLinksControl>" %>

Assuming your view for the ArticleController's default action also accepts a model that holds the information about the category name, you can send the category name to your user control this way :

<%Html.RenderPartial("~/Views/Shared/YourControl.ascx",
    new NameSpace.ArticleLinksControl { 
        CategoryName = Model.Category}); %>

Now in your usercontrol you can access the category name with Model.CategoryName.

This is if you insist on using a usercontrol for this. You can also get away with using Html helpers on your view.

çağdaş