asp.net MVC cascading dropdown lists

前端 未结 2 1859
礼貌的吻别
礼貌的吻别 2020-12-12 02:27

I\'m trying to create some cascading dropdown lists in asp.net. The lists are populating correctly on page load:

            
相关标签:
2条回答
  • 2020-12-12 02:56

    Add contentType:'application/json' in your ajax call.

    $.ajax({
                    type: 'POST',
                    url: '@Url.Action("GetRegionsByCountryCode")',
                    dataType: 'json',
                    contentType:'application/json'
                    data: { countryCode: $("#Country").val() },
                    success: function (data) {
                        $.each(data, function (index, value) {
                            $("#Regions").append('<option value="'
                             + value.CountryCode + '">'
                             + value.RegionName + '</option>');
                        });
                    },
                    error: function (ex) {
                        alert('Failed to retrieve regions.' + ex);
                    }
                });
    
    0 讨论(0)
  • 2020-12-12 02:59

    Your drop down id is Region but in your success function you are using Regions

    Make this one small change and it will work.

    Controller:

    public class MyCountry
    {
        public int CountryCode { get; set; }
        public string Country { get; set; }
    }
    
    public class Region
    {
        public int CountryCode { get; set; }
        public string RegionName { get; set; }
    }
    
    public class ViewModel
    {
        public List<MyCountry> Country { get; set; }
        public List<Region> Region { get; set; }
    }
    
    public class DropDownExampleController : Controller
    {
        public ActionResult Index()
        {
            var model = new ViewModel();
    
            var c1 = new MyCountry { Country = "South Africa", CountryCode = 1 };
            var c2 = new MyCountry { Country = "Russia", CountryCode = 2 };
    
            model.Country = new List<MyCountry> { c1, c2 };
    
            var r1 = new Region { RegionName = "Gauteng", CountryCode = 1};
            var r2 = new Region { RegionName = "Western Cape", CountryCode = 1 };
            var r3 = new Region { RegionName = "Siberia", CountryCode = 2 };
    
            model.Region = new List<Region> { r1, r2,r3};
            return View(model);
        }
    
        [HttpPost]
        public JsonResult GetRegionsByCountryCode(string countryCode)
        {
            var r1 = new Region { RegionName = "Gauteng", CountryCode = 1 };
            var r2 = new Region { RegionName = "Western Cape", CountryCode = 1 };
            var r3 = new Region { RegionName = "Siberia", CountryCode = 2 };
    
            var regions = new List<Region> { r1, r2, r3 };
    
            var results = regions.Where(r => r.CountryCode == Int32.Parse(countryCode)).ToList();
    
            return Json(results);
        }
    }
    

    View:

    @model MVCTutorial.Controllers.ViewModel
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#Country").change(function () {
    
                var countryCode = $("#Country").val();
                $("#Region").empty();
    
                $.ajax({
                    type: 'POST',
                    url: '@Url.Action("GetRegionsByCountryCode")',
                    data: { countryCode: countryCode },
                    success: function (data) {
                        $.each(data, function (index, value) {
                            $("#Region").append('<option value="'
                                                 + value.CountryCode + '">'
                                                 + value.RegionName + '</option>');
                        });
                    },
                    error: function (ex) {
                        alert('Failed to retrieve regions.' + ex);
                    }
                });
                return false;
            })
        });
    </script>
    
    <div class="form-group">
        Country
        @Html.DropDownListFor(m => m.Country, new SelectList(Model.Country, "CountryCode", "Country"), "--Select--", new { @class = "form-control" })
    </div>
    <div class="form-group">
        Region
        @Html.DropDownListFor(m => m.Region, new SelectList(Model.Region, "CountryCode", "RegionName"), "--Select--", new { @class = "form-control" })
    </div>
    
    0 讨论(0)
提交回复
热议问题