Reputation: 19267
I am new to Microsoft.MVC, so sorry if this is a silly question.
I have created a very simple forum, and I am now trying to create some tag functionality to it.
I have a controller where the index retrieves the last 10 forum threads. I would like to pass a query string, or something a-like with the Id to the supplied tag to the forum, so I thereby can get the forum threads, which e.g. have the tag 'ASP.NET'.
If it was a regular webforms project I would simply supply a query string with the tag id, to the index page, and then retrieve the forum threads with the tag, but isn't there a smarter way to do it in MVC.NET?
The reason why I ask, is it seems like a step backwards from REST-urls, to suddenly use 'regular' query strings?
Upvotes: 0
Views: 1269
Reputation: 8013
First you define your action (like you probably already did), and add the parameters like you need them:
public ActionResult Forum(string tag, int page)
{
// do your thing
// ...
return View();
}
Then, in your Global.asax.cs, you can add a route that handles the parameters like you want them.
routes.MapRoute("Forum", "Forum/{tag}/{page}", new {controller = "Home", action = Forum"});
This will cause the Forum action to trigger on the HomeController when you go to the http://yourhost/Forum link. If you then click have a link like this http://yourhost/Forum/asp.net/1 Then "asp.net" will passed into the tag parameter, and 1 will be passed onto the page parameter.
Upvotes: 1
Reputation: 4313
You can use an ActionLink html helper. Assuming you have a forums controller and index page, to get a link to /forums/index/1?tag=asp.net you can do:
Html.ActionLink("ASP.NET", "index", new { id = 1, tag = "asp.net"})
Upvotes: 0