州と都市のドロップダウンリストを作成しようとしています。州が選択されると、都市は移住します。私はこれをajaxを使ってやっています。私は書かれたすべてを持っている、私はそれが動作するように感じ、それはもちろんありません。状態のドロップダウンリストは、50のすべての州の列挙可能なリストを含む別のクラスから引き出されます。 2番目のドロップダウンリストとして、コントローラにJsonのデータを返して都市のドロップダウンに移すことを期待して書きました。ここ は私のjQueryとAJAXと一緒にdropdownlistforさんのための私のかみそりである:ここではajax経由でビューからコントローラへのフォームフィールドの値を渡し、状態の選択に基づいてドロップダウンの都市リストを設定します
<div class="form-group">
@Html.LabelFor(model => model.State, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.State, WebGridProject.Models.StateCodes.GetStatesList(), new { id = "State", @class = "form-control", @onchange = "FillCity2()" })
@Html.ValidationMessageFor(model => model.State, "", new { @class = "text-danger" })
</div>
</div>
<script>
function FillCity2() {
var stateId = $('#State').val();
console.log(stateId);
$.ajax({
url: '/Home/FillCity2',
type: "GET",
dataType: "JSON",
data: { State: stateId },
success: function (city) { // cities is the variable in the controller made to fetch the city list
$("#City").html("");
$.each(city, function (i , cityNames) {
$("#City").append(
$('<option></option>').val(cityNames.cities));
});
}
});
}
</script>
<div class="form-group">
@Html.LabelFor(model => model.City, htmlAttributes: new {@class = "control-label col-md-2"})
<div class="col-md-10">
@Html.DropDownListFor(model => model.City, new SelectList(Enumerable.Empty<SelectListItem>()), "Select City", new { id = "City", @class = "form-control"})
@Html.ValidationMessageFor(model => model.City, "", new {@class = "text-danger"})
</div>
</div>
は、私は、要求を処理するために使用していますコントローラのアクションです:
public JsonResult FillCity2(string stateName)
{
List<SelectListItem> cities = new List<SelectListItem>();
switch (stateName)
{
case "AK":
cities = new List<SelectListItem>
{
new SelectListItem() {Text = "", Value = ""},
new SelectListItem() {Text = "Anchorage", Value = "Anchorage"},
new SelectListItem() {Text = "Fairbanks", Value = "Fairbanks"},
};
break;
case "AL":
cities = new List<SelectListItem>
{
new SelectListItem() {Text = "", Value = ""},
new SelectListItem() {Text = "Auburn", Value = "Auburn"},
new SelectListItem() {Text = "Birmingham", Value = "Birmingham"},
new SelectListItem() {Text = "Dothan", Value = "Dothan"},
new SelectListItem() {Text = "Mobile", Value = "Mobile"},
};
break;
case "AZ":
cities = new List<SelectListItem>
{
new SelectListItem() {Text = "", Value = ""},
new SelectListItem() {Text = "Pheonix", Value = "Pheonix"},
new SelectListItem() {Text = "Flagstaff", Value = "Flagstaff"},
new SelectListItem() {Text = "Prescott", Value = "Prescott"},
new SelectListItem() {Text = "Tucson", Value = "Tucson"},
};
break;
case "AR":
cities = new List<SelectListItem>
{
new SelectListItem() {Text = "", Value = ""},
new SelectListItem() {Text = "Fayetteville", Value = "Fayetteville"},
new SelectListItem() {Text = "Fort smith", Value = "Fort smith"},
new SelectListItem() {Text = "Jonesboro", Value = "Jonesboro"},
new SelectListItem() {Text = "Texarkana", Value = "Texarkana"},
};
break;
default:
cities = new List<SelectListItem>
{
new SelectListItem() {Text = "", Value = ""},
new SelectListItem() {Text = "Please Select State", Value = "Please Select State"},
};
break;
}
return Json(cities, JsonRequestBehavior.AllowGet);
}
私はデバッガでの値を実行すると、 #Stateはnullとして表示されますが、コンソールのクロムには実際の値が表示されます(例:アラバマの場合は「AL」、アラスカの場合は「AK」など)。
値を渡すことで私の問題がどこにあるのかよく分かりません。そして、デバッガの都市リストは、デフォルトのスイッチの場合にとどまります。
アイデア?
:また、私はネイティブ
add()
方法を使用することをお勧めします。 – Shyju最後の質問に自分のコメントを繰り返すには - [this DotNetFiddle](https://dotnetfiddle.net/1bPZym)を参照して、カスケーディングドロップダウンリストを実装する方法を理解してください –
私はそれを見ました。私が探しているよりもはるかに多くです。また、私はそれを動作させることにいくつかの問題がありました。私はそれを私に見せてくれてありがとう。 – Devoneous