Ahmet Kaçmaz
Ahmet Kaçmaz

Reputation: 3

ASP.NET MVC 2 paging Route Problem

I get error when paging,

my actions:

ProductList(string country, string city, string town, int? pageNumber)

my Route:

routes.MapRoute(
    "ProductList",
    "myList/{country}/{city}/{town}/{pageNumber}",
    new { controller = "Product", action = "ProductList", country="", city="", town= "", pageNumber = UrlParameter.Optional });

Action Link:

Url.Action("myList","Product", new{ country="Finland",city="",town="",pageNumber=2 }) 

city = 2 ??

I've found a solution as follows:

Url.Action("myList","Product", new{ country="Finland",city="s",town="n",pageNumber=2 })

http:/myList/Finland/s/n/2

ProductList(string country, string city, string town, int? pageNumber)
{
    city== "s" ? city = null;
    town == "n" ? town= null;

    process...
}

to be http: /myList/Finland/2 /myList/Finland/Helsinki/3 /myList/Finland/town/7

Upvotes: 0

Views: 393

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039328

Only the last parameter in your route definition can be optional.

Upvotes: 0

Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38488

You cannot skip the parameters in that route. You just can't call http://mylist/2 and expect pageNumber to take value 2. Value in first segment is assigned to the first variable in the route. So 2 is assigned to city variable. You have to make sure all parameters before pageNumber gets a non-null value.

Upvotes: 0

Related Questions