您的 View 和 Model 不正确,实现目标的一种方法是创建一个 ViewModel 来处理您的两个列表,这样您就可以保留您的基本模型抽象。
这就是我将如何重新排列您的代码。
型号
using System;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.Web.Mvc;
namespace Alundra.Models
{
public class CountyCityViewModel
{
public List<CountyModel> Countymodel { get; set; }
public SelectList SelectedCity { get; set; }
}
public class CountyModel
{
public int ID { get; set; }
public string CountyName { get; set; }
}
public class CityModel
{
public int ID { get; set; }
public string CityName { get; set; }
//Foreign key for the County model
public int CountyID { get; set; }
public virtual CountyModel County { get; set; }
}
}
控制器
using System;
using System.Web.Mvc;
using System.Collections.Generic;
using Alundra.Models;
using System.Linq;
namespace HelloWorldMvcApp
{
public class HomeController : Controller
{
public ActionResult Index()
{
CountyCityViewModel CountyCityviewmodel = new CountyCityViewModel();
CountyCityviewmodel.Countymodel = new List<CountyModel>();
CountyCityviewmodel.Countymodel = GetAllCounties();
return View(CountyCityviewmodel);
}
//This is the action you will call using AJAX from your view
[HttpPost]
public ActionResult GetCityByCountyID(int CountyID)
{
List<CityModel> ListOfCities = new List<CityModel>();
ListOfCities = GetAllCities().Where(s => s.CountyID == CountyID).ToList();
SelectList SelectListOfCities = new SelectList(ListOfCities, "ID", "CityName", 0);
return Json(SelectListOfCities);
}
// Populate counties collection Or you can extract from DB
public List<CountyModel> GetAllCounties()
{
List<CountyModel> Countymodel = new List<CountyModel>();
Countymodel.Add(new CountyModel { ID = 0, CountyName = "Select a county" });
Countymodel.Add(new CountyModel { ID = 1, CountyName = "Freedom county" });
Countymodel.Add(new CountyModel { ID = 2, CountyName= "Not so good county" });
Countymodel.Add(new CountyModel { ID = 3, CountyName = "Best county" });
Countymodel.Add(new CountyModel { ID = 4, CountyName = "Crazy county" });
Countymodel.Add(new CountyModel { ID = 5, CountyName = "Happy county" });
return Countymodel;
}
//Populate cities collection Or you can extract from DB
public List<CityModel> GetAllCities()
{
List<CityModel> CityModel = new List<CityModel>();
CityModel.Add(new CityModel { ID = 1, CountyID = 3, CityName = "City1-1" });
CityModel.Add(new CityModel { ID = 2, CountyID = 3, CityName = "City2-1" });
CityModel.Add(new CityModel { ID = 3, CountyID = 2, CityName = "City4-1" });
CityModel.Add(new CityModel { ID = 4, CountyID = 2, CityName = "City1-2" });
CityModel.Add(new CityModel { ID = 5, CountyID = 1, CityName = "City1-3" });
CityModel.Add(new CityModel { ID = 6, CountyID = 5, CityName = "City4-2" });
CityModel.Add(new CityModel { ID = 7, CountyID = 5, CityName = "City4-2" });
CityModel.Add(new CityModel { ID = 8, CountyID= 1, CityName = "City4-2" });
CityModel.Add(new CityModel { ID = 9, CountyID = 2, CityName = "City4-2" });
CityModel.Add(new CityModel { ID = 10, CountyID = 4, CityName = "City4-2" });
return CityModel;
}
}
}
最后是你的观点
@model Alundra.Models.CountyCityViewModel
@{
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<script>
function GetCitieslist(Countyid) {
var procemessage = "<option value='0'> Please wait...</option>";
$("#ddlcity").html(procemessage).show();
var url = '@Url.Action("GetCityByCountyID")';
$.ajax({
url: url,
data: { CountyID: Countyid },
cache: false,
type: "POST",
success: function (data) {
var markup = "<option value='0'>Select City</option>";
for (var i = 0; i < data.length; i++) {
markup += "<option value=" + data[i].Value + ">" + data[i].Text + "</option>";
}
$("#CitiesDropDown").html(markup).show();
},
error: function (reponse) {
alert("error : " + reponse);
}
});
}
</script>
<h4><i class="fa fa-connectdevelop fa-3x pull-left fa-border"></i><span class="label label-warning">Cascade Dropdown menues using JQuery</span></h4>
@using (Html.BeginForm())
{
@Html.DropDownListFor(m => m.Countymodel, new SelectList(Model.Countymodel, "ID", "CountyName"), new { @id = "StateDropDown", @onchange = "javascript:GetCitieslist(this.value);" })
<br />
<br />
<select id="CitiesDropDown" name="ddlcity" style="width: 200px">
</select>
}
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
EXTRA:您可以在城市 DD 获取数据时实现加载图像。
这是一个工作示例的.net fiddle link