If you are using jQuery (which is very likely with the MVC project):
// lambda to get the ID of your dropdown. This runs on the server, // and injects the ID into a client-side variable. var id = '@Html.IdFor( o => o.model )'; // get the dropdown by ID and wrap in a jQuery object for easy manipulation var dropdown = $("#" + id); // get the value var value = dropdown.val();
Of course, you can combine this into one line if you want.
If you are not using jQuery, see fooobar.com/questions/1150 / ....
var id = '@Html.IdFor( o => o.model )'; var dropdown = document.getElementById( id ); var value = dropdown.options[dropdown.selectedIndex].value;
Guide Id:
@Html.dropDownList(m=>m.model, new SelectList(m.myList, "value", "text"), new {id = "ddl1"}) var value = $("#ddl1").val();
source share