Solved

Mvc Listbox Click Event in jQuery

Posted on 2010-09-23
6
4,325 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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 …
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now