The improved MVC double -drop -down list loading method
One way to load two drop -down lists (states and cities) in the MVC is to use the AJAX -level pull -up list method. The following is the improved code version:
Controller
View (using non -invasive javascript)
<code class="language-c#">/// <summary> /// 获取州列表 /// </summary> /// <returns>州列表,SelectListItem类型</returns> private IEnumerable<SelectListItem> GetStates() { using (var db = new DataEntities()) { return db.States.Select(d => new SelectListItem { Text = d.StateName, Value = d.Id.ToString() }); } } /// <summary> /// 获取指定州的城市列表 /// </summary> /// <param name="id">州ID</param> /// <returns>城市列表,JSON格式</returns> [HttpGet] public ActionResult GetCities(int id) { using (var db = new DataEntities()) { var data = db.Cities.Where(d => d.StateId == id).Select(d => new { Text = d.CityName, Value = d.Id }).ToList(); return Json(data, JsonRequestBehavior.AllowGet); } }</code>
In this method, we use the operation method to retrieve the cities selected in the state and return it as JSON. On the client, we use non -invasive JavaScript to handle the
incident of the state drop -down list and dynamically fill the city's drop -down list. This method provides a better user experience without a complete page refresh. The code was improved, the<code class="language-html">@model Person @{ ViewBag.Title = "Home Ajax"; IEnumerable<Person> persons = ViewBag.Persons; IEnumerable<SelectListItem> States = ViewBag.States; } @section featured { <div class="content-wrapper"> <hgroup class="title"><h1>@ViewBag.Title</h1></hgroup> </div> } @section styles { td, th { border: 1px solid; padding: 5px 10px; } select { padding: 5px 2px; width: 310px; font-size: 16px; } } @section scripts { @Scripts.Render("~/bundles/jqueryval") <script> var cityStates = @Html.Raw(Json.Encode(ViewBag.CityStates)); // 将CityState JSON转换为JavaScript对象 $(document).ready(function () { $("#ddlState").change(function () { loadCities(this); }); loadCities($("#ddlState")[0]); // 页面加载时初始化城市下拉列表 }); function loadCities(obj) { var stateId = parseInt($(obj).val()); var cities = cityStates.find(v => v.Id === stateId)?.Cities; if (cities) { fillCity(cities); } else { $("#ddlCity").empty(); // 清空城市下拉列表 } } function fillCity(cities) { $("#ddlCity").empty(); // 清空城市下拉列表 $.each(cities, function (index, city) { $("#ddlCity").append($("<option></option>").val(city.Id).text(city.CityName)); }); } function Edit(obj, Id) { $("input[name='Id']").val(Id); var tr = $(obj).closest("tr"); $("#txtfirstName").val($("td[data-id='fn']", tr).text().trim()); $("#txtlastName").val($("td[data-id='ln']", tr).text().trim()); $("#txtemail").val($("td[data-id='email']", tr).text().trim()); $("#ddlState").val($("td[data-id='cn'] input[type='hidden']", tr).val()); loadCities($("#ddlState")[0]); // 更新城市下拉列表 } </script> } <h3></h3></code>
was processed, and the filling method of the urban drop -down list was optimized. The initialization operation of the page loading is added to ensure that the city drop -down list is also correctly filled when the page is loaded. GetCities
The above is the detailed content of How to Efficiently Load Cascading State and City Dropdowns in MVC using AJAX?. For more information, please follow other related articles on the PHP Chinese website!