Shekar.gvr Shekar.gvr - 21 days ago 5
Javascript Question

Bootstrap multiselect dropdown .val() not updating

I have 3 drop downs which are cascading.
Zone -- > Region --> Territory

I am using Bootstrap multiselect drop downs.

When i select the Zone drop down the respective regions are to be bound
and simultaneously the newly bound regions' territories are to be bound to the territory drop down.

Here is my drop-down initialisation code.

$('#ddlZone').multiselect({
enableClickableOptGroups: true,
enableCollapsibleOptGroups: true,
enableFiltering: true,
includeSelectAllOption: true,
nonSelectedText: 'Select Zone',
enableCaseInsensitiveFiltering: true,
selectAllNumber: true,
onChange: function(option, checked,select) {
FillRegionsDropdown();
FillTerritoriesDropdown();

}


Here is the code for the above functions.

function FillRegionsDropdown()
{


var Zone=$('#ddlZone').val();
if(Zone != null)
{
Zone= Zone.join(",");
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "@Url.Action("BindRegionsOnZonesAjax", "GeoMap")",
data: "{ZoneIds:'" + Zone + "'}",

success: function (Result)
{


$("#ddlRegion").html("");

$('#ddlRegion').multiselect( 'refresh' );
$.each(Result, function (key, value) {
$("#ddlRegion").append($("<option></option>").val(value.Value).html(value.Text));
});
$('#ddlRegion').multiselect( 'rebuild' );
$("#ddlRegion").multiselect('selectAll', false);
$("#ddlRegion").multiselect('updateButtonText');

}


});

}
}


The above code works perfectly i.e. on Zone dropdowns change the regions get bound and set to select all.

But the issue is with Binding territory drop down with zone drop down change.

And here is the code for Territory dropdowns binding.

function FillTerritoriesDropdown()
{

var rgns=$('#ddlRegion').val();
if(rgns != null)
{
rgns= rgns.join(",");
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "@Url.Action("BindTerritoriesOnRegionsAjax", "GeoMap")",
data: "{RegionIds:'" + rgns + "'}",

success: function (Result)
{


$("#ddlTerritory").html("");

$('#ddlTerritory').multiselect( 'refresh' );
$.each(Result, function (key, value) {
$("#ddlTerritory").append($("<option></option>").val(value.Value).html(value.Text));
});
$('#ddlTerritory').multiselect( 'rebuild' );
$("#ddlTerritory").multiselect('selectAll', false);
$("#ddlTerritory").multiselect('updateButtonText');

}


});

}
}


Here the
$('#ddlRegion').val()
doesn't get updated to the newly bound region values which is caused due to zone drop down change.

$('#ddlRegion').val()
still contains the initial page load region values.

I have been struck with this for more than 6 hours now.

Can some one help me to fix this issue?,

Answer

Try using async:false in both FillTerritoriesDropdown() and FillREgionsDropdown() functions. i.e. in the ajaxcalls to the controller of those 2 functions.