1

currently I have a medium sized MVC project with multiple controllers that support paging through a paging helper.

this works like this

routes.MapRoute(
                "TabletsPaged",
                "Tablet/Page/{page}",
                new { controller = "Tablet", action = "Index" }
            ); //paging in the tablet controller

in my master page i have a nav with a link to all of the index actions of my various controllers. For whatever reason when I change pages on any of my controllers my nav links become /controller/page/x where x is the current page i am viewing, and when in the home controller i the links just appear as /controller. Any ideas why this is going on?

1
  • Please post the ActionLinks you use in your pager and in your navigation. Commented Sep 21, 2009 at 21:35

1 Answer 1

2

The routing engine takes the values of placeholder variables in the current route (the view you are on) and automatically adds them to all links generated in that view which also contain these variables.

You need to explicitly reset these variables where you don't need the page numbers, that is, when you generate the links for your menu, like this:

ActionLink ("Tablet", "Index", new { page = "" });

To your problem with weird link: look at the ActionLink signature - you're feeding it the wrong parameter sequence.

To your use this overload gets hit:

ActionLink (string linkText, string actionName, object routeValues,
            object htmlAttributes);

As the third parameter it expects the RouteValues but instead receives a string, while the fourth parameter expects the htmlAttributes and you're giving it the route values.

Use another overload:

ActionLink (string linkText, string actionName, string controllerName,
            object routeValues, object htmlAttributes);

And just give it "null" as the fifth argument if you're not setting any html attributes.

ActionLink("Patients", "Index", "Patient", new {page = ""}, null)
Sign up to request clarification or add additional context in comments.

4 Comments

actually one problem, when putting in html.ActionLink("Patients", "Index", "Patient", new {page = ""}) i get some weird link that doesnt work
I updated my explanation to be more precise on what is happening.
still not working, I'm doing my paging view routes not get variables if that clears it up any
never mind i got it, thank you very much! just needed to add a controller declaration in the new {} section

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.