Search code examples
asp.netasp.net-mvc-2default-valueparameter-passingactionmethod

Default value for parameter in Controller Method is overriding everything


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

Solution

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