0

ASP MVC WebGrid renders pagination links like

http://host/AnyController/AnyAction?Length=4&page=1

Any chance to parametrize or customize this to

http://host/AnyController/AnyAction/1

to conform better to MVC routing conventions?

(NOT important: btw Length is redundant. If the controller's Action method does not know the page length, by heart that's problem. )

Thanks in advance

g.pickardou
  • 32,346
  • 36
  • 123
  • 268
  • Can you add your existing route definitions? – Brent Mannering Sep 17 '14 at 23:57
  • @Brent: Do you mean if I would have a route definition for say parameter page, then WebGrid will render the link according to that? And what is the parameter name? 'page'? – g.pickardou Sep 18 '14 at 00:05
  • @g-pickardou yes and no, adding a route with the `page` parameter will mean that the route will pass the value to your action correctly, however when it comes to matching the route on the incoming request, the `page` route will conflict with your `default` route. In short the `page` route has to be unique i.e. `http://host/AnyController/AnyAction/page/1` – Brent Mannering Sep 18 '14 at 03:20

1 Answers1

0

One way to handle your default route conflict can be this, as most of the time when you use you route it will hit the action method without the [HttpGet]. All you need to do is a GET, whenever you sort or page the web gird, it will try to get data and hit the a HttpGet Action, this will work as follows:

    [HttpGet]
    public ActionResult YourActionMethod()
    {
        return PartialView("YourView",YourModel);
    }

The best part is, upon sorting, the request will send a parameter named as "sortBy" too, you can use this here and decide what you want to do with the binded Model with the grid. You can inspect what URL the Sort header will hit by using the "Developer Tools" in your browser. You can use this action do as you will,

Note : By default the action method it would be hitting would be same as the controller name.

Utkarsh Bais
  • 197
  • 8