views:

27

answers:

2

I'm using the dropdownlist html helper and would like to perform an ajax call to the server when the value changes. I have seen the jquery code dropdownlist.change...

The problem I have is that I have a series of dropdownlist's name dropdownlist_1, dropdownlist_2, ...

I would like to be able to specify the same jquery function for each of these dropdownlist's. I have not been able to find a way to specify the function name in the dropdownlist html helper.

Thanks, Henry

A: 

Instead of attempting to specify it in the HtmlHelper, add a class name to each of your dropdown lists:

<%=Html.DropDownList("ddlName", new {@class="ajaxDropDown"})%>

or

<select class="ajaxDropDown" id="ddlOne">
  <option>1</option>
  <option>2</option>
  <option>3</option>
</select>
<select class="ajaxDropDown" id="ddlTwo">
  <option>Red</option>
  <option>Orange</option>
  <option>Blue</option>
</select>

Then use the classname to hook up the event via jQuery:

$('.ajaxDropDown').change(function() {
  $.get('/test', function(data) {
    ...
  });
});
Peter J
A: 

try this:

<%=Html.DropDownList("TopItemsList", ViewData["ListData"], new { @onchange="javascript();" })%> 

you can call the function you want.

moi_meme