Solved

Mvc Listbox Click Event in jQuery

Posted on 2010-09-23
6
4,390 Views
Last Modified: 2013-11-08
Hello

I have used the Html.ListBox helper to generate a <select> list in my view like so:

<%= Html.ListBox("MaterialTypesList", Model.MaterialsList, new { @class = "StockListBox", SelectionMode="Single"})%>

Open in new window


in jQuery I have attached an event handler (not on the change event but the click event) to capture the value of the clicked item to proceed with it:

           
$("#MaterialTypesList").click(MaterialsList_Changed);

Open in new window


However, each time I click a value, it does not correspond, and the first click is not taken into account:

function MaterialsList_Changed()
            {
                $("#ModelsList > option").remove();

                var materialId = $("#MaterialTypesList option:selected").val();
                alert(materialId);
                $.post("http://localhost/" + virtualPath + "/Stock/LoadBrands/" + materialId, null, function (data)
                {
                    data = $.map(data, function (item, a)
                    {
                        return "<option value=" + item.Value + ">" + item.Text + "</option>";
                    });

                    $("#BrandsList").html(data.join(""));
                });
            }

Open in new window


My questions:

1) why doesn't this work, it looks like it should be easy simple
2) why does the html attribute SelectionMode on the Listbox not work ?

thx !

Gerrie
0
Comment
Question by:gerrie-govaerts
  • 3
  • 3
6 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33744719
-- use change event
http://api.jquery.com/change/

-- also are you sure that id is MaterialTypesList only,

-- try the class selector
$(".StockListBox").change(MaterialsList_Changed);

-- last but not the least, put this code inside document ready event handler

$(document).ready(function(){
  $(".StockListBox").change(function(){
MaterialsList_Changed()
});
});
0
 

Author Comment

by:gerrie-govaerts
ID: 33751738
Hello

I have used everything that's in your code, and I cannot use the change event - I already discussed this in another issue, it must be the click event.

It always has worked using a normal <select> tag, but now I'm using the HtmlHelper to generate a listbox and nothing works anymore....
0
 

Author Comment

by:gerrie-govaerts
ID: 33751923
To add some more information:

I made a difference between the working select list (hand typed) and the Html.ListBox helper method (which I prefer of course).

The main difference is that the attribute multiple="multiple" makes things crash in my case.

How can I use the Html.ListBox method and specify that I DON'T want to have the multiple="multiple" attribute ?

Thx !
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 33751989
0
 

Author Closing Comment

by:gerrie-govaerts
ID: 33752016
Thank you ! You saved my day ;-)
It's quite unusual to think about a dropdownlist when you need a listbox, but in fact... it remains a select tag
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33752028
true, not sure why microsoft decided to go this way, since all it differs is in the fact that one has multiple attribute, and other doesn't have.

thanks for the points :)
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
There is a huge demand for CodeIgniter among the PHP web developers due to its dynamic features and benefits these days. It is one of most popular and agile open source PHP framework for creating robust web applications in PHP web development field.…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
The viewer will learn how to dynamically set the form action using jQuery.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question