0

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!

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You could do like this:

$("#yourselectid").change(function(){
  window.location = "bike/" + $(this).val();
});

2 Comments

That would be the easiest way, but not very maintainable. :/ What if for example the route changes?
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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.