Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
JavaScript
<script type="text/javascript">
    $("#ReportId").change(function () {
        $.ajax({
            url: "/RLMaster/GetHeadName",
            data: { ReportId: $('#ReportId').val() },
            type: "GET",
            dataType: "json",
            success: function (data, textStatus) {
                alert(data);
                loadData(data);
            },
            error: function () {
                alert("Failed! Please try again.");
            }
        });
    });
</script>


This is my Javascript. I am able to retrieve data from database but unable to populate my DropDown List with that data.
Please help. Also tell me the HTML which I should write for the dropdown
Posted
Updated 31-Aug-15 3:02am
v3
Comments
Amit Jadli 31-Aug-15 8:20am    
you could use normal dropdownlist and access it from jquery and append options in it like below.
$('#dropdownId').append('<option value="+value+">"+text+"</option>');
loop through the values if it is more then one.
Member 11579819 31-Aug-15 8:26am    
Ahh..Ok
And how to write that dropdown list command in html with null values. I am very new to this and yet I have used dropdown only with Viewbags.
If possible please elaborate your answer. That would be a great help for me
Amit Jadli 31-Aug-15 8:31am    
<asp:DropDownList ID="drpList" runat="server">
<asp:ListItem Value="">Select


if you are using asp.net webforms this is the code of HTML for dropdownlist..
Member 11579819 31-Aug-15 8:40am    
I am using Razor actually
Amit Jadli 31-Aug-15 8:44am    
<select id="selCountry"><option value="0">Select</option></select>

1 solution

Hi,

You can do with below code

1) first add below in html

@Html.DropDownList("ddldrpid", new SelectList(new List<selectlistitem>()), "Select State/Province")


2) now return selectlistitem from controller(make sure that its return List<selectlistitem> from json request).

3) After that in sucess part do it below code to bind drop down:

if (data.length > 1) {
               $("#ddldrpid").empty();

               var selectList = $("#ddldrpid");
               $.each(data, function (index, item) {
                   selectList.append($('<option />', {
                       value: item.Value,
                       text: item.Text,
                   }));
               });
           }



like that way you can bind drop down using jason result.

let me know if you need more help.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900