0

都市と国のドロップダウンリストを作成しようとしています.ドロップダウン国が選択されている場合、ドロップダウン国は国の都市を表示します..

これは私のコード

コントローラ

        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);
        }

ドロップダウンからViewModelに値を送信する私の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;
        }

意見

<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>
                }

セッションを使用してドロップダウンからデフォルト値を指定しようとしましたが、うまくいきません。コントローラーからデフォルト値を指定しますが、国のドロップダウンだけです。国でデフォルト値が「イタリア」でデフォルト値が「ローマ」の場合、国と都市からデフォルト値を与えるにはどうすればよいですか

4

2 に答える 2