Skip to content Skip to sidebar Skip to footer

Populate Drop Down List With Jquery On Button Click In Mvc

I am very new to jQuery, is there by any chance some code using jQuery that I can use to populate a drop-down list with in MVC? I am using MVC models to populate drop-down lists at

Solution 1:

You have to append <option> to a select. Not just a string. Also, you are using team.name. In your result set, there is no "name" property on each object. There is however team.Text.

$("#LeagueTeams").append($("<option>" + team.Text + "</option>"));

$(function() {
    $(".dropdownlistLeagueStyle").change(function() {
        $.getJSON("/Admin/GetTeams", {
            LeagueId: $(".dropdownlistLeagueStyle").val()
        }, function(results) {
            $("#LeagueTeams").empty();
            $.each(results, function(i, team) {
                // append an option with the team name in there
                $("#LeagueTeams").append($("<option>" + team.Text + "</option>"));
            });
        });
    });
});

I have made a jsFiddle using the results you are getting and a select.

jsFiddle

On another note, you might want to change your return type on the controller action to JsonResult instead of ActionResult.

Post a Comment for "Populate Drop Down List With Jquery On Button Click In Mvc"