0

i try to make drop down list for city and country.. when dropdown Country was select, drop down country show city in country..

this my code

Controller

        public ActionResult Customer()
        {
            List<Country> country = _CustomerService.GetCountryForDropDown();
            CustomerViewModel model = CustomerBuilder.BuildCountries(country);
            return View(model);
        }

        [HttpPost]
        public ActionResult GetCityByCountry(int CountryID)
        {
            List<City> city = _CustomerService.GetCityForDropDown();
            CustomerViewModel model = CustomerBuilder.BuildCities(city);

            List<DDCity> getCityFromCountry = new List<DDCity>();
            getCityFromCountry = model.DDCityModel.Where(r => r.CountryID == CountryID).ToList();
            SelectList CityinCountry = new SelectList(getCityFromCountry, "CityID", "CityName", 0);
            return Json(CityinCountry);
        }

My viewModel and Function to submit value from Dropdown to ViewModel

public class DDCountry
    {
        public int CountryID { get; set; }
        public string CountryName { get; set; }
    }

    public class DDCity
    {
        public int CountryID { get; set; }
        public int CityID { get; set; }
        public string CityName { get; set; }
    }
public class CustomerViewModel
    {
        public List<CustomerItem> ListCustomer { get; set; }
        public CustemerRequest Request { get; set; }
        public EditCustomer EditCustomer { get; set; }
        public List<DDCountry> DDCountryModel { get; set; }
        public List<DDCity> DDCityModel { get; set; }
        public SelectList GetCity { get; set; }

    }

 public static CustomerViewModel BuildCities (List<City> city)
        {
            CustomerViewModel model = new CustomerViewModel();
            model.DDCityModel = new List<DDCity>();

            foreach (var cities in city)
            {
                DDCity ct = new DDCity
                {
                    CityID = cities.CityID,
                    CityName = cities.CityName,
                    CountryID = cities.CountryID
                };
                model.DDCityModel.Add(ct);
            }
            return model;
        }

        public static CustomerViewModel BuildCountries(List<Country> country)
        {
            CustomerViewModel model = new CustomerViewModel();
            model.DDCountryModel = new List<DDCountry>();

            foreach (var countries in country)
            {
                DDCountry ct = new DDCountry
                {
                    CountryID = countries.CountryID,
                    CountryName = countries.CountryName
                };
                model.DDCountryModel.Add(ct);
            }
            return model;
        }

View

<script type="text/javascript">
    function GetCity(_CountryID) {
        var procemessage = "<option value='0'> Please wait...</option>";
        $('#ddlCity').html(procemessage).show();
        var url = "/MasterDataCustomer/GetCityByCountry/";

        $.ajax({
            url: url,
            data: { CountryID: _CountryID },
            cache: false,
            type: "POST",
            success: function (data) {
                var markup = "<option value='0'>Select City</option>";
                for (var x = 0; x < data.length; x++) {
                    markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
                }
                $('#ddlCity').html(markup).show();
            },
            error: function (reponse) {
                alert("error : " + reponse);
            }
        });
    }
</script>

@using (Html.BeginForm("", ""))
                {                    
                    @Html.Label("Country - City")
                    @Html.DropDownListFor(m => m.DDCountryModel, new SelectList(Model.DDCountryModel, "CountryID", "CountryName"), new { @id = "ddlstate", @style = "width:100px;margin-left:38px;", @onchange = "javascript:GetCity(this.value);" })
                    <select id="ddlCity" name="ddlCity" style="width: 100px; margin-left: 5px;"></select>
                }

i try to give default value from drop down using session, but, its not work. i give default value from my controller, but, just in my DropDown for country. how can i give default value from country and city, when in Country, default value is "Italia", and the default value is "Rome", and DropDown City can show all City in "Italia"

novian kristianto
  • 751
  • 3
  • 12
  • 34
  • 1
    This is not a good way of creating cascading drop-down lists. Here you can find a better solution: http://stackoverflow.com/questions/5497524/easiest-way-to-create-a-cascade-dropdown-in-asp-net-mvc-3-with-c-sharp – ataravati Aug 01 '13 at 03:49

2 Answers2

0

You can have a field in the country called defaultCityId .And also a property to the City IsSelected. Then when you are building the city list just set the specific default city IsSelected property to true. And then in the client side,

for (var x = 0; x < data.length; x++) {
  markup += "<option selected="+data[x].IsSelected? "Selected":""+ " value=" + data[x].Value + ">" + data[x].Text + "</option>";
}
Jayantha Lal Sirisena
  • 21,216
  • 11
  • 71
  • 92
0

this way you can set default value for your dropdownlist

Model

 public class CustomerModel
    {

    public int CustomerNameId { get; set; }

    public string customerIdName { get; set; }

    public List<SelectListItem> CustomerNameIdList { get; set; }
 }

Controller

 model.CustomerNameIdList.Insert(0, (new CustomerModel { CustomerNameId = 0, customerIdName = " No Organization Size" }));
Jaimin
  • 7,964
  • 2
  • 25
  • 32