Cascading (Dependent) DropDownList in ASP.Net MVC

Last Reply 14 days ago By dharmendr

Posted 14 days ago

Hello there,

I am using MVC ASPNET and I have three cascading dropdownlist and it working the first time.

So I am able to select the first dropdown and filter the result on the second and by selecting second dropdown the third dropdown is filtered.

But when I go back and change the selection in the first drop down, it is filtering the second drop down but not deleting the old values and accoding the values of first selection with values of new selection.

This is the article

Thanks for help.

Vincenzo

You are viewing reply posted by: dharmendr 14 days ago.
Posted 14 days ago

Use below code.

function PopulateDropDown(dropDownId, list) {
    var modal = $('<div />');
    modal.addClass("modalBackground");
    $('body').append(modal);
    var loading = $(".loading");
    loading.show();
    var top = Math.max($(window).height() / 2 - loading[0].offsetHeight / 2, 0);
    var left = Math.max($(window).width() / 2 - loading[0].offsetWidth / 2, 0);
    loading.css({ top: top, left: left });
    setTimeout(function () {
        if (list != null && list.length > 0) {
            $(dropDownId).removeAttr("disabled");
            $.each(list, function () {
                $(dropDownId).empty().append('<option selected="selected" value="0">Please select</option>');
                $(dropDownId).append($("<option></option>").val(this['Value']).html(this['Text']));
            });
            $(".loading").hide();
            $('.modalBackground').remove();
        }
    }, 1000);
}