0

2 つのドロップダウンがあります。最初はCountryで、2 番目はCityです。

カスケード ドロップダウンを使用して検索しようとすると、結果が表示されると、ドロップダウンの値が再び null ではなくなります。例として、 とでオーストリアを選択するCountryと、結果が表示されたとき、ドロップダウンの値は stillとisです。ViennaCityCountryAustriaCityVienna

JavaScriptを使おうとしていますが、できません。この私のコード:

コントローラ

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

        [HttpPost]
        public ActionResult Customer(CustemerRequest request, FormCollection frm)
        {
            if (ModelState.IsValid)
            {                
                List<Organization> ListOrganization = _CustomerService.GetOrganization(request.InfotechCode, request.CustomerName, frm["DDCountryModel"], frm["ddlCity"]);
                List<Country> country = _CustomerService.GetCountryForDropDown();
                cvm = CustomerBuilder.Build(ListOrganization,country);
                cvm.Request = request;
                return RedirectToAction("Result", new { Page = 1 });
            }
            return View(new CustomerViewModel { Request = request, ListCustomer = null });
        }

        [HttpGet]
        public ActionResult Result(int Page)
        {
            if (cvm != null)
            {
                return View("Customer", cvm);
            }
            else
                return RedirectToAction("Customer", new { Request = Request });
        }

        [HttpPost]
            public ActionResult GetCityByCountryForDropDown(int CountryID, FormCollection frm)
            {
                List<City> city = _CustomerService.GetCityForDropDown();
                ITTOS.ViewModel.Customer.CustomerViewModel model = ITTOS.ViewModel.Builder.CustomerBuilder.BuildCities(city);

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

ビューモデル

[Serializable]
    public class CustemerRequest
    {
        public string InfotechCode { get; set; }
        public string CustomerName { get; set; }
        public int? CountryID { get; set; }
        public int? CityID { get; set; }
    }

    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 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 SelectedCountry = $('#CountryID').val(_CountryID);
                var procemessage = "<option value='0'> Please wait...</option>";
                $('#ddlCity').html(procemessage).show();
                var url = "/Json/GetCityByCountryForDropDown/";


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

        function GetValue(_CityID)
        {
            $('#CityID').val(_CityID);
        }
    </script>

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

誰か助けてくれませんか

4

1 に答える 1