asp.net-mvc ASP.NET MVC 3 DropDownList selectedindexchanged
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7952547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC 3 DropDownList selectedindexchanged
提问by Paul
I have a piece of functionality that allows users to filter records based on their own status codes. In the menu, I have a custom filters section:
我有一个功能允许用户根据他们自己的状态代码过滤记录。在菜单中,我有一个自定义过滤器部分:
<h3>Custom Filters</h3>
<br />
<ul id="ui-ajax-tabs">
@{ Html.RenderAction("GetGroups", "Manage");}
</ul>
And my partial view looks like this:
我的部分视图如下所示:
@model IEnumerable<AllEngage.Model.Group>
@using AllEngage.Web.Helpers
@foreach (var group in Model)
{
<li>
<label for="@group.GroupName">@group.GroupName</label>
@Html.DropDownList("GroupItems", group.GroupItems.ToSelectListItems())
</li>
}
When an item is selected from a dropdownlist, I want an action method to fire in my controller:
当从下拉列表中选择一个项目时,我希望在我的控制器中触发一个操作方法:
[HttpGet]
public ActionResult Index(int page = 1, int groupFilterId = -1)
What would be the best way to go? Fire using json or perform a post back somehow?
最好的方法是什么?使用 json 触发还是以某种方式执行回发?
回答by Samich
You need to pass you param to action. The fastes way to do this - pass via query string with the same name as param in action:
您需要将参数传递给操作。执行此操作的快速方法 - 通过与 param 同名的查询字符串传递:
http://mysite/GetGroups?groupFilterId=2
To refresh it you need to send ajax request with param whitch will be fired onchangeof the dropdown control. Specify some ID for your filter control:
要刷新它,您需要发送带有参数的 ajax 请求,将触发onchange下拉控件。为您的过滤器控件指定一些 ID:
@Html.DropDownList("GroupItems", group.GroupItems.ToSelectListItems(), new {@id="ddlFilter"})
and then using jQuery make a GET request of your data:
然后使用 jQuery 对您的数据进行 GET 请求:
$('#ddlFilter').change(function() {
var queryLink = '@Url.Action("GetGroups")';
if ($(this).val() != '') {
queryLink += '?groupFilterId=2';
}
$.get(queryLink, function(data) {
$('#ui-ajax-tabs').html(data);
});
});
回答by user1006544
you need to fire ajax call using jquery in click event of dropdown. First chek on click event of dropdown if the value changes then call a jquery ajax call I am pasting the code of jquery .
您需要在下拉菜单的单击事件中使用 jquery 触发 ajax 调用。如果值发生变化,首先检查下拉列表的单击事件,然后调用 jquery ajax 调用我正在粘贴 jquery 的代码。
$.get("/Wk/Revision/@hTitle/@pageID/?langID=2", function (data) {
$("div#dAjaxContent").html(data);
$("div#dShow").css("visibility", "visible");
$("#dShow").dialog();
});
// this is for ajax call . the dropdown check do it yourself. "/Wk/Revision/@hTitle/@pageID/?langID=2" this will call the controller method revision . and next i think you will do.
// 这是用于 ajax 调用。下拉检查自己做。"/Wk/Revision/@hTitle/@pageID/?langID=2" 这将调用控制器方法 revision 。接下来我想你会做的。
回答by Brian Mains
I'd recommend doing a post request; you can build one using this:
我建议做一个发布请求;您可以使用此构建一个:
http://iridescence.no/post/Invoking-ASPNET-MVC-Actions-from-JavaScript-using-jQuery.aspx
http://iridescence.no/post/Invoking-ASPNET-MVC-Actions-from-JavaScript-using-jQuery.aspx
You could make an AJAX get request, or if you want to redirect away, you can change the JavaScript's window.location to point to the action you want, as in:
您可以发出 AJAX get 请求,或者如果您想重定向,您可以更改 JavaScript 的 window.location 以指向您想要的操作,如下所示:
window.location = "/MyController/Index";

