views:

41

answers:

2

I have a drop down list (<select> tag) filled with bikes (more specifically their names). When a user selects a bike in the list I want them to be redirected to the details page for the selected bike.

I have a route for bike details as such:

routes.MapRoute("BikeDetails",
                "bike/{bikeId}",
                new {
                    controller = "Bike",
                    action = "Details"
                },
                new { bikeId = @"\d+" });

What is the best way solve this?

I'm thinking maybe I should use the URLs for the drop down list item values and then use JQuery to redirect to the selected URL. But having dealt with old plain ASP.NET before, I can't help but think the guys at Microsoft have thought something clever out for scenarios such as this one.

Any ideas appreciated!

A: 

You could do like this:

$("#yourselectid").change(function(){
  window.location = "bike/" + $(this).val();
});
igorti
That would be the easiest way, but not very maintainable. :/ What if for example the route changes?
Deniz Dogan
Well, one step towards making this more maintainable could be to create a global JSON object on the server-side at runtime which would contain routename/routeurl. Then in your javascript you could write something like window.location = approutes["BikeDetails"] + $(this).val();Exactly how you implement this functionality is another question :)
igorti
+2  A: 

I think your jQuery idea is really the way to go, though I'd probably not tie it to the select's change event, but have a button that triggers the redirect. Some browsers, especially using screen readers, don't behave well with the change event - firing it for each item as you scroll over it. Since it's taking you away from the page, that might be a really bad thing for some users.

You probably also need to have the select wrapped in a form that posts back to the action and a route that allows you to get to that action via the post. That action can accept the URL as a parameter and issue the redirect to the URL. This will cover you in the case where javascript is turned off.

tvanfosson