asp.net - How to pass a selected dropdown value to the MVC controller? -
this ajax method , mvc controller. dropdownlist in url "http://localhost:1424/home/drop". after navigating, choose state dropdownlist , when navigate url "http://localhost:1424/home/getstates", json data null. json on url {"selectedcountyid":null}. please assist.!!!!!!!!!!!!!!!!!
$('.ddlcountry').change(function () { $('#ddlstate').empty(); $.ajax({ type: "post", datatype:"json", url: "home/getstates", data: { id: $('.ddlcountry').val() }, }); }); public actionresult getstates(string id) { return json(new { selectedcountyid = id }, jsonrequestbehavior.allowget); }
your using class selector, may finding multiple elements in dom.
also advise use url.action
generate urls, don't string way url cause trouble in future.
modify jquery code :
$('.ddlcountry').change(function () { $('#ddlstate').empty(); var selected = $(this).val(); // current dropdown element selected value $.ajax({ type: "post", datatype:"json", url: '@url.action("getstates","home")', data: { id: selected }, }); });
Comments
Post a Comment