views:

56

answers:

3

Let's say I have a ActionResult method that has a pageNumber parameter and a category parameter.

The user should be able to set the category he's browsing which would be a ActionLink to the first page of that category. However if I have an another ActionLink where I go to the next page the category parameter would go back to default.

How can I set the category parameter to be the same as on the previous page.

EDIT: Originally supposed to be a comment, but grow in size

Sorry, I see my question isn't clear enough wrote it in a kind of hurry. I'm kinda trying to do what you are doing but instead of setting the category as 1 I wan't it set as the parameter was when this page was loaded.

Scenario: I am browsing the website and click "shoes" (categoryID = 3) wich has this actionlink <%=Html.ActionLink("Shoes", "Index", new { categoryID = 3 }) %>

I get the first page of shoes and then I wan't to skip to the next page which has a actionlink <%=Html.ActionLink(">>>", "Index", new { page = model.currentPage + 1 }) %> I can't include categoryID parameter in this actionlink code because in my generic view code I of course don't know which category is active.

So when I click on that it switches to the default category again.

A: 

It sounds like you need to add the category id as a property of your model object so it is available in the view. So your view code would look like this:

<%=Html.ActionLink(">>>", "Index", new { categoryId = model.categoryId, page = model.currentPage + 1 }) %>
KOTJMF
I see my question wasn't clear enough so I added a little. Thank you.
Ingó Vals
Yes this is one possibility.
Ingó Vals
A: 

Like the answer above parameters will be provided from a URL like http://www.mysite.com/catalog?category=1&amp;page=2 . Remember these have to match with your Action Method parameter names. Here's an action method example:

//I assumed method should be reached even if no parameter was provided, since it's an Index method
//Parameters are string so they can be null. You can use int? or convert them.
public ActionResult Index(string category, string page)
{
    //I wrote a basic template, you will fill the gaps and provide data for views
    if (String.IsNullOrEmpty(category))
        return View("Main");//for main Index
    else
    {
        if (String.IsNullOrEmpty(page))
            return View("CategoryMain");//for the Index of the category
        else
            return View("CategoryPage");//for pages with given category
    }
}

If http://www.mysite.com/catalog?category=1&amp;page=2 looks ugly, you can define a custom route for a prettier URL.

Ufuk Hacıoğulları
A: 
Ingó Vals
I could have also stored it in ViewData.
Ingó Vals