3

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

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

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.

Thanks.

1 Answer 1

4

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.
            }
        });
});

UPDATE:

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.
            }
        });
   }
});

You can have a look at this fiddle.

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

8 Comments

Thanks, the listbox is actually wrapped up in a Jquery control. To access the values I want I do as follows: onItemClick: function (checkbox, selector) { alert("value " + checkbox.val() + ", is checked: " + checkbox.prop("checked")). How would I pass the two values back into the controller using this method? Thanks for your help.
Apologies if it seems a simple question, my first day using JQuery :)
@Darren Young: no worries. Do you want to submit your FORM when the user selects an element from the list or you're happy to do an ajax call?
@@Darren Young: also, are you using some kind of jquery plugin which wraps your listbox and transforms it into a list of checkboxes?
@LeftyX, thanks. Yes, the jquery plugin wraps any control that is in effect an HTML select control. The code in my previous comment shows how to get the data from it. In effect when the user checks an item in the list, then it would call the database and update other lists within the view. Is it even possible to just update other controls within the same view? Thanks
|

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.