views:

13

answers:

2

Hello i have just started learning mvc2 and im having a problem with the default value for the parameter page(you can see the method below).

Its always 0 regardless of what i type in the URL. For example this

h.ttp://localhost:52634/Products/List/2

should show page 2 but when in debug mode the page parameter is 0, so im always getting the first page of the list in my view.

i am using the predefined standard routes in global asax when you start a new mvc2 project.

am i missing something?

//This is the ProductsController

   public ViewResult List(int page = 0)
    {

        var products = productsRepo.Products()

   //send in source, current page and page size
        productList = new PagedList<Product>(products, page, 10);

        return View(productList);
    }
A: 

Hey,

Remove the " = 0", and do:

public ViewResult List(int? page)
{
    int val = page.GetValueOrDefault(0);

And use val everywhere instead of page. That should work. If not, it's an issue with routing.

HTH.

Brian
A: 

It's a routing issue, the default route specifies an id property, you're using a property called page. I'm new to MVC myself, but add this route before the default route:

routes.MapRoute("MyRoute", "{controller}/{action}/{page}",
    new { controller = "Foo", action = "List", page = UrlParameter.Optional });
GenericTypeTea
omg im such a doof, i was just staring at the routing in my global.asax not even paying notice to the {id} :P Thanks for your answer :D
Kimpo
@Kimpo - No problem!
GenericTypeTea