Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi all,
I really tired of searching solution for my problem.My problem is very simple.I want to set a value for dropdownlist using jquery ajax.But it doesnt works for me.I am using MVC.

What I have tried:

<select id="ddlRelationship" class="form-control chosen-select" name="ddlRelationship">
</select>


JQuery is:
$(document).ready(function () {
    
   
    refill()
   

});


function refill() {

    
    var id = $('#HdnOrganizationId').val()
   // alert(id);
  
      $.ajax({

        url: "/Organization/refill/" + id,
        type: "GET",
        contentType: "application/json;charset=utf-8",
        dataType: "json",

        success: function (result) {
            alert("Refill");
            alert(result.RelationshipId);
            relen();
             $('#TxtOrganization').val(result.RelationshipId);
             $('#ddlRelationship').val(result.RelationshipId);
           
        },
        error: function (errormessage) {
            alert(errormessage.responseText);
        }
    });
}


function relen() {
    $.getJSON("/Common/RelationshipList", function (data) {


        alert ('y56hyh')
        var temp = $('#ddlRelationship'); // cache it
        temp.empty();
        $("#ddlRelationship").append("<option value=''>Select an Option</option>");

        $.each(data, function (i, data) {      // bind the dropdown list using json result              
            $('<option>',
               {
                   value: data.RelationshipId,
                   text: data.Relationship
               }).html(data.Relationship).appendTo("#ddlRelationship");
        });
        $('#ddlRelationship').trigger("chosen:updated");
    })
}


Through this code Textbox is refilled.,and dropdown binded but canot set a value.pls help me.
Posted
Updated 1-Jul-18 22:10pm
v3

1 solution

refill() and relen() both are async calls. You're trying to set the values in your first call. Hence,
$('#TxtOrganization').val(result.RelationshipId);
$('#ddlRelationship').val(result.RelationshipId);
Write this code in your relen() success, after data is bound to dropdown.
Pass result.RelationshipId as a parameter to relen().

KR
 
Share this answer
 
v2
Comments
Nijisha Kc 2-Jul-18 4:24am    
where should i write the success function in relen()?
Krunal Rohit 2-Jul-18 4:25am    
function relen() {
$.getJSON("/Common/RelationshipList", function (data) {


alert ('y56hyh')
var temp = $('#ddlRelationship'); // cache it
temp.empty();
$("#ddlRelationship").append("Select an Option");

$.each(data, function (i, data) { // bind the dropdown list using json result
$('',
{
value: data.RelationshipId,
text: data.Relationship
}).html(data.Relationship).appendTo("#ddlRelationship");
});
$('#ddlRelationship').trigger("chosen:updated");
here set the value
})
}


KR
Nijisha Kc 2-Jul-18 4:44am    
I tried this.But still it is not working
Krunal Rohit 2-Jul-18 7:00am    
error ?

KR
Nijisha Kc 3-Jul-18 5:08am    
didnt get any error.but it is not setting value.

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