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.

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.