Skip to content Skip to sidebar Skip to footer

Mvc Dropdownlist - Capturing Change Event Without Attaching The Event Handler

I know I can attach change event to html.dropdownlist in mvc as shown below: $('#ddList').change(function() { var value = $(this).val(); }); However, is

Solution 1:

Yes, you were close, as the onchange attribute was what you were looking for.

<%= Html.DropDownList("ddList", Model.dropDown, new { @class = "Ddl", onchange = "ddListChange" }) %>

jQuery skips the on part of the event names in its methods/parameters for events, e.g. .click(...) / .bind('click',..) equals the onClick attribute.

Binding event handlers using javascript itself is the preferred way. Just like you separate styling into separate CSS files, you separate scripting into your own scripting files. This allows the HTML file to be cleaner and easier to read, in addition to separating the concerns of prensentation and whatever your scripts is doing. It also makes it easier for you page to degrade gracefully (work even if scripts aren't enabled).

Here is one article discussing the issue


Post a Comment for "Mvc Dropdownlist - Capturing Change Event Without Attaching The Event Handler"