javascript - Populate DropDownList by JSON -
-- controller --
[webmethod] public actionresult getsellers() { list<seller> sellers = db.sellers.tolist(); return json(sellers, jsonrequestbehavior.allowget); }
-- view --
@html.dropdownlistfor(x => x.sellerid, new selectlist(enumerable.empty<selectlistitem>()))
-- javascript --
<script type="text/javascript"> $('#deptid').change(function () { // deptid dropdownlist $.getjson('/salerecords/getsellers'), null, function (result) { // path var ddl = $('#sellerid'); // seller ddl ddl.empty(); $('sellers').show(); // div (it's display: none) $(result).each(function () { ddl.append( $('<option />', { value: this.id }).html(this.name) ); }); }; }); </script>
what's wrong? i've debugged in controller, , sellerslist has 3 registries, doesn't appear in view, what's problem?
this might issue
$.getjson('/salerecords/getsellers'), null, function (result) // should throw error
try
$.getjson('/salerecords/getsellers') .done(function(result) { var ddl = $('#sellerid'); ddl.empty(); $('sellers').show(); $(result).each(function () { ddl.append( $('<option />', { value: this.id }).html(this.name) }); .fail(function(jqxhr) {console.log(jqxhr.responsetext)});
Comments
Post a Comment