重定向到控制器/视图时的DropDownList改变 [英] Redirect to controller/view when dropdownlist changed

查看:77
本文介绍了重定向到控制器/视图时的DropDownList改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET MVC应用程序。我有一个Html.DropDownList。我想重定向到另一个控制器时,选择改变,希望所选的DropDownList项目的键作为URL参数一个

ASP.NET MVC application. I have a Html.DropDownList. I want to redirect to another controller when the selection is changed, and want the key of the selected dropdownlist item as a param in the URL.

我应该如何去吗?

感谢

推荐答案

您需要使用带有JavaScript来做到这一点;我会建议使用jQuery。此外,把下拉菜单的形式,获取和控制器/动作URL的行动方法。然后设置click事件发布的形式。

You'll need to do this with Javascript; I would recommend using jquery. Also, put the dropdown in a form, method of get and action of the controller/action URL. Then set the click event to post the form.

所以,你的HTML将是这样的:

So, your HTML will be something like:

<form id="myForm" action="/Controller/Action" method="get">
    <select id="mySelect">...</select>
</form>

和jQuery中是这样的:

And in jquery something like:

$('#mySelect').change(function() { $('#myForm').submit(); });

请注意,你可以使用的Html.Form()辅助函数来创建表单。另外,我建议有一个提交表单,您可以用JavaScript隐藏按钮。然后让JS禁用谁仍然让人有一种方式来提交表单。

Note you can use the Html.Form() helper to create the form. Also, I'd recommend having a submit button on the form, which you can hide with javascript. Then people who have JS disabled still have a way to submit the form.

这篇关于重定向到控制器/视图时的DropDownList改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆