Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Mvc Listbox Click Event in jQuery

Posted on 2010-09-23
6
Medium Priority
?
4,568 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:Gurvinder Pal Singh
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 2000 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:Gurvinder Pal Singh
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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 the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
Suggested Courses

773 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