简体   繁体   English

ASP.NET MVC:重定向用户以使用下拉列表中的参数进行路由

[英]ASP.NET MVC: Redirect user to route with parameters from a drop down list

I have a drop down list ( <select> tag) filled with bikes (more specifically their names). 我有一个下拉列表( <select>标记),里面充满了自行车(更具体地说是它们的名字)。 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. 我在想,也许我应该使用URL作为下拉列表项的值,然后使用JQuery重定向到选定的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. 但是,在接触过旧的普通ASP.NET之前,我不禁认为Microsoft的人员已经为这种情况想到了一些巧妙的方法。

Any ideas appreciated! 任何想法赞赏!

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. 我认为您的jQuery想法确实是可行的方法,尽管我可能不会将其与select的change事件联系在一起,但是有一个按钮可以触发重定向。 Some browsers, especially using screen readers, don't behave well with the change event - firing it for each item as you scroll over it. 某些浏览器(尤其是使用屏幕阅读器的浏览器)在change事件中表现不佳-在滚动每个项目时将其触发。 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. 该操作可以接受URL作为参数,并发出对URL的重定向。 This will cover you in the case where javascript is turned off. 这将在关闭javascript的情况下为您服务。

You could do like this: 你可以这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM