I am having issues with populating a telerik dropdown list using jquery. I get the following Error:
‘get(…).options’ is null or not an object
On line:
$(“#courseID”).get(0).options.length = 0;
Here is my telerik dropdown list:
<%= Html.Telerik().DropDownList()
.Name("courseID")
.HtmlAttributes(new{ @id="courseID"})
%>
Here is how I am trying to populate it using jquery:
I call the populateDDL function on “StudentID” focusout event, here is the function.
function populateDDL() {
var link = '/Student/EnrollerCourse';
$.ajax({
type: 'POST',
url: link,
data: { id: $('#StudentId').val() },
dataType: 'json',
success: function (result) {
$("#courseID").get(0).options.length = 0;
$("#courseID").get(0).options[0] = new Option("Select", "Select");
$.each(result.courseID, function (item, value) {
var courseID = value;
$("#courseID").get(0).options[$("#courseID").get(0).options.length] = new Option(courseID);
});
},
error: function (result) {
alert("Failed")
}
});
}
Any help is appreciated
Update 8/24
Sorry for the confusion. As i said – i had created a Visual C# – Web Project – of type “RadControls for Web Application”. Here is a screenshot:
The modified Site.Master code looks like below:
Notice that i added a JQuery reference through a script tag. That was the only change i did. Rest of the code stays as is.
I have zipped up my code and uploaded to the following location :
http://sdrv.ms/T1EdBK
You can download the same and have a look at the code. You will need to set reference to Telerik.Web.Mvc.dll from you system
Hope this solves your problem
Telerik MVC Extension controls have a rich client side API. So if you have used a DropDownList – you will not be able to get the control by using the jquery syntax $(“#id”). Instead you will need to use the following:
Here is code snippet i was able to spin up for this scenario of yours:
View:
So i have a drop down list defined with a name and id attributes. I have a button which is used to mimic your out of focus scenario. Idea is that on click of the button we will fire a AJAX request and get a JSON payload. we will use the JSON payload to bind the drop down list options.
JavaAcript:
As you can see the first line in the function is about getting a reference to the drop down list. Then assume that you made a AJAX request and you have got the JSON payload. You use the databind method to bind the JSON data. Then i use the select method to set the first option as the selection item in the drop down list.
This scenario is showcased in our online demo application page at the following URL:
http://demos.telerik.com/aspnet-mvc/combobox/clientsidebinding
Hope this answers your question.
Lohith (Tech Evangelist, Telerik India)