Binding dropdownlist with jquery ajax in asp.net
Aspx Page:
$.ajax({
type: "POST",
url: "EBService.aspx/getDepts",
data: "{cid:" + reg + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$("#<%=ddlCDept.ClientID%>").get(0).options.length = 0;
$("#<%=ddlCDept.ClientID%>").get(0).options[0] = new Option("Select name", "0");
var data=jQuery.parseJSON( msg.d);
$.each(data, function(index, item) {
$("#<%=ddlCDept.ClientID%>").get(0).options[$("#<%=ddlCDept.ClientID%>").get(0).options.length] = new Option(item.c_dept_name, item.c_dept_id);
});
C# Code:
[WebMethod]
public static List<CDept_MstrBO> getDepts(int cid)
{
if (CDeptList == null)
{
CDept_MstrBO objDeptBo = new CDept_MstrBO();
objDeptBo.companyID = 1;
CDeptList = objDeptBo.getCDeptList(objDeptBo);
}
List<CDept_MstrBO> deptList = CDeptList.Where(d => d.c_id == cid).ToList();
return deptList;
}
Labels:
ASP.NET