c# - asp.net MVC 4How to capture json data from action and update dropdonwlist -
background: have asp.net mvc4 web application. in page, got drop down list filled file inforamation. try letting user click button, , fire popup window in which, user can enter name , select file upload. similar how upload picture in stackoverflow.
once file uploaded completed. in action, return json data include filename , uploaded time.
return json(new { error = false, uploadedfilename = filename, uploadedtime = datetimeoffset.now }
question: unfortunately, action returns print json data on page.
could me 1. how can view capture json data , update dropdown list instead of showing them on page? 2. should use ajax (jquery ajax or ajax.beginform) handle this?
thanks regards
you can achieve follows:
in controller pass json data:
return json(new[] { new { id = "value1", name = "name1" }, new { id = "value2", name = "name2" } }, jsonrequestbehavior.allowget);
then make , ajax call recive json response , can populate dropdown follows:
$.ajax({ type: //get or post depends on need, contenttype: 'application/json;charset=utf-8', url: //your url, data: // pass data if any, datetype: 'json', success: function (data) { var list = $('select#id'); list.find('option').remove(); $(data).each(function(index, item) { list.append('<option value="' + item.id + '">' + item.name + '</option>'); }); } });
Comments
Post a Comment