请帮助我填充我的级联下拉列表时遇到问题。 请参阅下面的代码。我有一个获取Suburbs的方法,但它在JavaScript中没有异常。
PUT
这是我在VIEW中的代码
public ActionResult LoadTown()
{
List<SelectListItem> li = new List<SelectListItem>();
li.Add(new SelectListItem { Text = "Select", Value = "0" });
li.Add(new SelectListItem { Text = "ALBERTON", Value = "1" });
li.Add(new SelectListItem { Text = "BEDFORDVIEW", Value = "2" });
li.Add(new SelectListItem { Text = "BENONI", Value = "3" });
li.Add(new SelectListItem { Text = "BOKSBURG", Value = "4" });
li.Add(new SelectListItem { Text = "BRAKPAN", Value = "5" });
li.Add(new SelectListItem { Text = "EDENVALE", Value = "6" });
li.Add(new SelectListItem { Text = "GERMISTON", Value = "7" });
li.Add(new SelectListItem { Text = "KEMPTON PARK", Value = "8" });
li.Add(new SelectListItem { Text = "NIGEL", Value = "9" });
li.Add(new SelectListItem { Text = "SPRINGS", Value = "10" });
li.Add(new SelectListItem { Text = "BRAKPAN", Value = "11" });
li.Add(new SelectListItem { Text = "EDENVALE", Value = "12" });
ViewData["town"] = li;
return View();
}
public JsonResult GetSurburbs(string id)
{
List<SelectListItem> states = new List<SelectListItem>();
switch (id)
{
case "ALBERTON":
states.Add(new SelectListItem { Text = "Select", Value = "0" });
states.Add(new SelectListItem { Text = "ALBERANTE", Value = "1" });
states.Add(new SelectListItem { Text = "AALBERTON", Value = "2" });
states.Add(new SelectListItem { Text = "ALBERTSDAL", Value = "3" });
states.Add(new SelectListItem { Text = "ALRODE", Value = "4" });
states.Add(new SelectListItem { Text = "ALRODE SOUTH", Value = "5" });
states.Add(new SelectListItem { Text = "ARTON", Value = "6" });
states.Add(new SelectListItem { Text = "BASSONIA ROCK", Value = "7" });
states.Add(new SelectListItem { Text = "BRACKENDOWNS", Value = "8" });
states.Add(new SelectListItem { Text = "BRACKENHURST", Value = "9" });
states.Add(new SelectListItem { Text = "EDEN PARK", Value = "10" });
states.Add(new SelectListItem { Text = "EDEN PARK WEST", Value = "11" });
states.Add(new SelectListItem { Text = "ELANDSFONTEIN 108-IR", Value = "12" });
states.Add(new SelectListItem { Text = "FLORENTIA", Value = "13" });
states.Add(new SelectListItem { Text = "GENERAAL ALBERTSPARK", Value = "14" });
states.Add(new SelectListItem { Text = "GREENFIELDS", Value = "15" });
states.Add(new SelectListItem { Text = "KLIPRIVIERSBERG 106-IR", Value = "16" });
states.Add(new SelectListItem { Text = "KERALA", Value = "17" });
请在此处查看Javascript代码
<div class="form-group">
@Html.Label("Select Town", new { @class = "col-md-2 control-label" })
<div class="col-md-12">
@Html.DropDownList("Town", new SelectList(new string[] { "ALBERTON", "BEDFORDVIEW", "BENONI", "BOKSBURG", "BRAKPAN", "EDENVALE" , "GERMISTON", "KEMPTON PARK", "NIGEL", "SPRINGS", "BRAKPAN", "EDENVALE" }), new { @class = "dropdown1"})
</div>
</div>
<div class="form-group">
@Html.Label("Select Surburb", new { @class = "col-md-2 control-label" })
<div class="col-md-12">
@Html.DropDownList("Select Surburb", new SelectList(string.Empty, "Value", "Text"), "Please select a Surburb")
</div>
</div>