views:

541

answers:

2

Basically if I wanted to make something a search page with paging I would need a url like:

/Topics/Index?search=hi&page=1

What I can't seem to figure out is how to:

A) Set a default route with no search and page 1 /Topics/Index?page=1 or even /Topics/Index?search=&page=1

B) use the View method to do the same

I do see that if I have a method on the control:

Index(String search, Int32? page)

And use the url:

/Topics/Index?search=hi&page=1 or /Topics/Index?search=hi

It gives me what I want in the method. I just need a way to get a default route for the Topic controller to create a default url with said request variables. I just don't think that

/Topics/Index/hi/1

Is conducive to a search url, mostly because there's no guarantee I'll have search terms or a page so it could end up like:

/Topics/Index/1

+1  A: 

Anything you pass in the RouteValueDictionary that doesn't map to a part of your Url will get added as a querystring parameter. So you can do:

Url.GenerateUrl("Route", "Index", "Topics", 
  new RouteValueDictionary(new 
    { 
      page = this.Model.PageNumber, 
      search = this.Model.Search
    });
Talljoe
So that's good for say a link or a "redirect" but what about setting up a route default for the page? Unless that works the same?
Programmin Tool
A: 

So basically I resorted to handling the non values by setting up defaults on the controller. Not sure this is the best idea though.

In GLobal.asax:

routes.MapRoute
(
 "TopicDefault",                                              
 "Topic/{action}",                          
  new { controller = "Topic", action = "Index"}  
);

On the Controller:

public ActionResult Index(Int32? parentForumId, Int32? pageNumber, Int32? amountToShow)
{

  Int32 revisedPageNumber = pageNumber.HasValue ? pageNumber.Value : 0;
  Int32 revisedAmountToShow = amountToShow.HasValue ? amountToShow.Value : 10;
  Int32 revisedParentForumId = parentForumId.HasValue ? parentForumId.Value : 1;

  IList<TopicCreateViewModel> modelValues =
     Topic.GetListForGrid(revisedParentForumId, revisedPageNumber, 
       revisedAmountToShow, out realPage)


  return View("Index", modelValues);
}
Programmin Tool