json + jquery ajax populate option -
i must populate option data inside html tag using json, i'm not sure how pass data.
the json
[{"citta_provincia":"agrigento - aragona","comune":"aragona"},{"citta_provincia":"alessandria - san michele","comune":"san michele"},{"citta_provincia":"ancona","comune":"ancona"}]
the html
<select class="field-s mandatory" id="dedaler_city" name="dealer_city"> <option selected>scegli...</option> </select>
the javascript
$cittasel = $('#dedaler_city'); $.ajax({ url: "http://path-to-json.json", datatype: 'html', success: function(data) { var parsa = $.parsejson(data); $.each(parsa, function(key, value) { $cittasel.html("<option value='" + value + "'>" + value + "'</option>'"); }); }, error:function(){ $cittasel.html('<option id="-1">not aviable</option>'); } });
i need know how pass value populate option fields, me?
in html, add class dropdown
cssclass="widget-dropdown-ajax-postback"
use jquery in javascript ajax dropdown
var ajaxtsddropdown = $("select.widget-dropdown-ajax-postback"); if (ajaxtsddropdown.length > 0) { ajaxtsddropdown.removeclass("gvi-widget-dropdown-ajax-postback"); ajaxtsddropdown.ajaxdropdown( { url: "/ajaxhelp/getitems" }); ajaxtsddropdown.on("ajaxdropdownselect", function (event, data) { if (data.select.val()) { $("#ajaxselected").val(data.select.val()); data.select.change(); } }); }
this select change , go json file render info. in ajax controller, add
public jsonresult getitems() { return json(controller.getdropdownitems(), jsonrequestbehavior.allowget); }
in code behind, call class
if (custompostback) { dropdown.cssclass = "widget-dropdown-ajax-postback"; }
this call json.
the json call go controller , retrieve items want retrieve them ( service call, entity framework, etc). data passed dropdown
if have service setup, call service. otherwise, replace service entity framework , loop through database.
public static listitem[] getdropdownitems() { var list = new list<listitem>(); list<itemsofinterest> itemsofinterest= new list<itemsofinterest>(); serviceadapter.invokeservice(ser => { itemsofinterest= ser.getitemsofinterest(); }); var items = itemsofinterest.select(cd => cd.itemnumber).distinct().tolist(); foreach (var item in items) { listitem currentitem = new listitem(itemnumber); list.add(currentitem); } list.insert(0, new listitem("select item")); return list.toarray(); }
Comments
Post a Comment