ASP.NET MVC列表框选择处理项事件 [英] ASP.NET MVC Listbox handling selected item events

查看:121
本文介绍了ASP.NET MVC列表框选择处理项事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的视图,作为这样的内HTML.ListBox:

I am using a HTML.ListBox within a view as such:

<%= Html.ListBox("projects", Model.Projects)  %>

这是填充了一个IEnumerable &LT; SelectListItem&GT; 模型这样:

This is populated with an IEnumerable<SelectListItem> in the model as such:

Projects = project.Select(a => new System.Web.Mvc.SelectListItem
        {
            Value = "foo",
            Text = a.project + "(" + a.count + ")",
            Selected = false
        });

        return Projects;

而这又被返回到控制器,用于通入强类型图。

Which in turn is returned to the controller for passing into a strongly typed view.

我的问题是,我怎么能监视选择项事件?我需要能够执行当用户进行选择和/或取消选择一些操作。

My question is, how can I monitor for select item events? I need to be able to perform some action when the user makes a selection and/or de-selection.

感谢。

推荐答案

如果你熟悉的jQuery,你可以捕获一个甚至改变并提交表单:

If you're familiar with jQuery you can trap a change even and submit your FORM:

$('#projects').change(function () {
    $(this).parents('form').submit();
});

您也可以使用阿贾克斯一些数据提交到一个动作

or you can use ajax to submit some data to an action

$('#projects').change(function () {
    $.ajax({
        type: 'POST',
        url: '<%=Url.Action("<action>", "<controller>")%>',
            data: { id: $('#projects').val() },
            dataType: 'json',
        complete: function(XMLHttpRequest, textStatus) {
            // Do something here.
            }
        });
});

更新

由于您使用 jQuery的下拉列表检查列表可以捕获onItemClick甚至和呼叫控制器的传递所选选项的值的动作和状态检查/非检查:

Since you're using jQuery Dropdown Check List you can trap the onItemClick even and call an action of your controller passing the value of the selected option and the status checked/non checked :

$("#projects").dropdownchecklist({
    emptyText: "select something",
    width: 150,
    onItemClick: function(checkbox, selector){
        var isChecked = checkbox.prop("checked");
        alert("element id: " + checkbox.prop("value"));

        $.ajax({
        type: 'POST',
        url: '<%=Url.Action("<action>", "<controller>")%>',
            data: { selecteId: checkbox.prop("value"), isChecked: isChecked },
            dataType: 'json',
        complete: function(XMLHttpRequest, textStatus) {
            // Do something here.
            }
        });
   }
});

您可以看看这个小提琴

这篇关于ASP.NET MVC列表框选择处理项事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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