JavaScript File Code
function bindDropDown(apiUrl, dropDownId) {
$.ajax({
type: "GET",
url: apiUrl,
dataType: "json",
success: function (result) {
var recordsDocument = jQuery.parseJSON(result);
//Assgin records value into drop down
$.each(recordsDocument, function (text, data) {
if (data == 0) {
$("#" + dropDownId + "").append("<option value=" + data.Value + " selected='selected'>" + data.Text + "</option>");
} else {
$("#" + dropDownId + "").append("<option value=" + data.Value + ">" + data.Text + "</option>");
}
});
}
});
}
ApiUrl page Code
public string GetDropDownListDocument()
{
try
{
List<SelectListItem> recordsDocument = new List<SelectListItem>();
recordsDocument =
_docStyleRepository.GetAll()
.OrderBy(c => c.DocDescription)
.AsEnumerable()
.Select(k => new SelectListItem
{
Text = k.DocDescription,
Value = k.Id.ToString(CultureInfo.InvariantCulture)
}).ToList();
var emptyItem = new SelectListItem { Selected = true, Value = "0", Text = "---Select---" };
recordsDocument.Insert(Utilities.ZERO, emptyItem);
return JsonConvert.SerializeObject(recordsDocument);
}
catch (Exception exception)
{
return null;
}
}
Change Code Accourding to your requirement. i am using this code on my project and it's working for me. if any query let me know.
Thanks & Regard
Shambhoo :)