Solved

Mvc Listbox Click Event in jQuery

Posted on 2010-09-23
6
4,372 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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Can anyone please tell me why i can not find ID  investorCoolGrid inside the jsp page? 1 19
Javasctipt 2 24
jQuery for Each 12 16
Else condition 9 14
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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 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…

816 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

9 Experts available now in Live!

Get 1:1 Help Now