Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind DropDownlists with JQuery in Asp.Net

Tags:

jquery

asp.net

I have 3 dropdownlists for Country,State and Metro. I want to when user seclect Country then State dropdownlist fill Jquery and when select Sate then Metro dropdownlist fill(like cascading dropdownlist of ajax).These process i want to do with JQuery.

like image 686
Vijjendra Avatar asked Oct 18 '09 18:10

Vijjendra


People also ask

How to bind DropDownList in mvc using jQuery?

Step 1: Just right-click on the Controllers folder and add a New Scaffolded item. Step 2: Select MVC Controller- Empty. Step 3: Enter the controller name as SampleController and modify the Index() with the code below: public ActionResult Index() {

How to load DropDown using jQuery AJAX in mvc?

First add your select tag to create dropdownlist in html. Now add your jQuery reference on the top as we know that to use jQuery we need to add the library on the top. Now create Index action which initiate the index page and another action named ShowData which returns the records in JSON format.

How can set the selected value of DropDownList using jQuery in ASP NET?

To set the selected value in drop down list: $('#<%=ddlVersion. ClientID%> option:selected'). text(currentVersion);


1 Answers

I am going to describe it in ASP.NET MVC, but the same can be achieved if you either write an ASP.NET web service or just put a few page methods in your code behind to do the same - you'll also need a JSON serializer, either a 3rd party solution or the one in WCF.

Using MVC, first, let's have three controller actions - one to display the page, countries will be static, and two to get the states and metros respectively:

public ActionResult Index()
{
    ViewData["Countries"] = _countryRepository.GetList();
    return View();
}

public ActionResult States(string countryCode)
{
    var states = _stateRepository.GetList(countryCode);
    return Json(states);
}

public ActionResult Metros(string countryCode, string state)
{
    var metros = _metroRepository.GetList(countryCode, state);
    return Json(metros);
}

In the view, you have three DropDownLists, one is bound to the ViewData["Countries"] object, say it's named Countries, you can get the states in jQuery with an Ajax call like this:

$('#Countries').change(function() {
    var val = $(this).val();
    $states = $('#States');
    $.ajax({
        url: '<%= Url.Action('States') %>',
        dataType: 'json',
        data: { countryCode: val },
        success: function(states) {
            $.each(states, function(i, state) {
                $states.append('<option value="' + state.Abbr+ '">' + state.Name + '</option>');
            });
        },
        error: function() {
            alert('Failed to retrieve states.');
        }
    });
});

The Metros drop down would be filled analogically, passing both the country and state selection to the server and getting back a JSON object with an array of metro areas.

I left out the details of repository implementation, just fill up the result variable on the server with a collection of states/metro areas in some way. I also made an assumption that the State class would have two properties - Abbr (e.g., 'CA') and Name (e.g., California).

I hope that it helps you in any way, or at least directs you somehow towards the solution.

like image 77
Pawel Krakowiak Avatar answered Oct 05 '22 05:10

Pawel Krakowiak