JQuery Resdownlist z listy MVC JSONRESULT
// mvc
List<SelectListItem> corgCodeLst = new List<SelectListItem>();
// List of carrier org code from Store procedure
var orgCodes = containerObj.GET_CW1_SCAC_ORGCODE(scac);
// Translate it to the selectlist item
foreach (var item in orgCodes)
{
corgCodeLst.Add(new SelectListItem
{
Text = item.SCAC,
Value = item.OrgCode
});
}
// return as Json , focus only on the orgcode , others is differnet purpose here
return Json(new { orgcode = corgCodeLst, ckey = cKey, cr = carrier, sc = scac, or = cOrgCode, r = result }, JsonRequestBehavior.AllowGet);
// view
$.ajax({
url: '@Url.Action("yourmethid", "mvccontroller")' ,
data: JSON.stringify(parm),
type: "POST",
dataType: "json",
contentType: 'application/json; charset=utf-8',
success: function (res) {
$('#IC_ContainerKey').val(res.ckey);
$('#txtCarrier').val(res.cr);
$('#txtSCAC').val(res.sc);
$('#ddlOrg').find('option').remove();
$(res.orgcode).each(function (index, item) {
$("#ddlOrg").append($('<option></option>').val(item.Text).html(item.Value));
});
$("#ddlOrg").chosen("destroy").chosen();
},
});
// html
<select class="dropdown" id="ddlOrg"></select>
BlackSwan