c# - not able to get values in second dropdownlist in a view -
i have got cascaded dropdown list show items in second drop downlists regarding selected ones in first drop downlist purpose have done this.... code controller
namespace mvcsampleapplication.controllers { public class cascadelistcontroller : controller { public actionresult index() { list<selectlistitem> state = new list<selectlistitem>(); state.add(new selectlistitem { text = "state1", value = "state1" }); state.add(new selectlistitem { text = "state2", value = "state2" }); viewbag.statename = new selectlist(state, "value", "text"); return view("basicdrop"); } public jsonresult districtlist(string id) { var districttype = s in cascadingdropdowns.getdistrictlist() s.statename == id select s; return json(new selectlist(districttype.toarray(), "statename", "districtname"), jsonrequestbehavior.allowget); } } }
and view
@scripts.render("~/bundles/jquery") <script type="text/javascript"> $(function () { $("state").change(function () { $.getjson('/cascadelistcontroller/districtlist' + $('#state').val(), function (data) { var items = '<option>select district</option>'; $.each(data, function (i, districttype) { items += "<option value = '" + districttype.value + "'>" + districttype.text + " </option> "; }); $('#district').html(items); }); }); }); </script> @model mvcsampleapplication.models.cascadingdropdowns @{ viewbag.title = "basicdrop"; } <h2>basicdrop</h2> @using (html.beginform()) { <div> <fieldset> <legend>dropdownlists</legend> @html.label("state") @html.dropdownlist("state", viewbag.statename selectlist,"select state", new {id= "state"}) @html.label("district"); <div class="mycombo"> <select id ="district" name ="distrct"></select> </div> </fieldset> </div> }
when select item in first drop down list not able see items in second 1 , have used div property second dropdown list increasing width have specified width '70px' not work..
would 1 pls give suggestions on one... many in advance.
two things wrong in url:
- missing slash "/" separating action name , id,
- word "controller" should removed
cascadelistcontroller
.
$.getjson('/cascadelist/districtlist/'+ $('#state').val(),
also, advisable use url.content
transform virtual server-side paths urls:
$.getjson('@url.content("~/cascadelist/districtlist/")'+ $('#state').val(),
Comments
Post a Comment