Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i have the three tables in sql server
1.Country
2.State
3.City
all tables have the primary key ,foreign keys.
now how can i fill the state dropdown on the click of Country dropdown and fill the city dropdown on the click of state dropdown in mvc3.
Thanks,
Posted
Updated 8-Dec-11 1:06am
v2

OnSelectedIndexChanged event of DropDownList control, you can fill the appropriate data in the list.

XML
<aspropdownlist cssclass="TxtBox" id="Project"  runat="server" tabindex="13" autopostback="true"  önselectedindexchanged="Country_SelectedIndexChanged"></aspropdownlist>



Country_SelectedIndexChanged event will fill the adjacent DropDownList controls State and City.
 
Share this answer
 
Comments
Dylan Morley 8-Dec-11 9:26am    
Not for MVC...
You could achieve this using jquery and a call from your view to a JSON action on your controller...

Some jquery in your View..

JavaScript
var url = '@Url.Action("ActionName", "ControllerName")';
$("#Country").change(function () {
    $.post(url, { countryId: $(this).val() }, function (data) {
        if (data.length > 0) {
            // fill your select box here with returned data...see 'fillSelect' method below
            $("#States").fillSelect(data, "Name", "Id", "[None]");
        }
    });
});


Your controller action...get some data from your table into a List (however you've wired data access) and use Select() to return just the Id and Description. I don't know what your state table fields are, I've guessed...!

C#
public JsonResult GetStates(int countryId)
{
    // Some data access method, gets a generic List<state> ??
    var list = SomeDataObject.GetStates(countryId);
     
    var states = list.Select(s => new {Id = s.StateId, Name = s.StateName});
    return Json(states);
}</state>


Now, when your #Country drop down list changes index, it will call GetStates and return some JSON to your 'Success' jquery handler. Here are a couple of helpers for clearing \ populating select lists. Just add them into a .js file and include them in your project

C#
// Jquery extensions
$.fn.clearSelect = function () {
    return this.each(function () {
        if (this.tagName == 'SELECT')
            this.options.length = 0;

    });

}
$.fn.fillSelect = function (data, text, value, defaultOptionText) {
    return this.clearSelect().each(function () {
        if (this.tagName == 'SELECT') {
            var dropdownList = this;
            if (defaultOptionText != null && defaultOptionText.length > 0) {
                var option = new Option(defaultOptionText, "");
                if ($.browser.msie) {
                    dropdownList.add(option);
                }
                else {
                    dropdownList.add(option, null);
                }
            }

            $.each(data, function (index, optionData) {
                var option = new Option(optionData[text], optionData[value]);

                if ($.browser.msie) {
                    dropdownList.add(option);
                }
                else {
                    dropdownList.add(option, null);
                }
            });
        }
    });
}
// End Jquery extensions
 
Share this answer
 

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