[英]Cascading drop down with KNOCKOUT.JS & ASP.NET MVC 4
我正在關注本教程:
http://www.dotnetexpertguide.com/2012/06/cascading-dropdown-knockoutjs-aspnet.html
該項目提供的工作就像一個魅力。 它可以從這里下載: http : //files.dotnetexpertguide.com/download.aspx?key = cascadingddlknockoutjs
問題是 - 我無法弄清楚如何更改視圖,以便再顯示一個城市選擇框,其內容會根據所選的狀態而變化?
為城市編寫另一個模型,並在控制器中按狀態ID獲取城市的操作非常簡單,但我不明白如何更改View和JS代碼以使其有效。
那么View的代碼:
<p>
<b>Select Country :</b> @Html.DropDownList("ddlCountry", ViewBag.Country as SelectList,"Select...", new { onchange = "FetchStates();" })
</p>
<p data-bind="visible: states().length > 0">
<b>Select State :</b> <select data-bind="options: states, optionsText: 'StateName', optionsValue: 'StateId', optionsCaption: 'Choose...'"></select>
</p>
<script type='text/javascript'>
function CascadingDDLViewModel() {
this.states = ko.observableArray([]);
}
var objVM = new CascadingDDLViewModel();
ko.applyBindings(objVM);
function FetchStates() {
var countryCode = $("#ddlCountry").val();
$.getJSON("/Home/GetStates/" + countryCode, null, function (data) {
objVM.states(data);
});
}
</script>
非常感謝任何幫助。
<p>
<b>Select Country :</b> @Html.DropDownList("ddlCountry", ViewBag.Country as SelectList,"Select...", new { onchange = "FetchStates();" })
</p>
<p data-bind="visible: states().length > 0">
<b>Select State :</b> <select id="ddlStates" data-bind="options: states, optionsText: 'StateName', optionsValue: 'StateId', optionsCaption: 'Choose...'"></select>
</p>
<p data-bind="visible: cities().length > 0">
<b>Select City :</b> <select data-bind="options: cities, optionsText: 'CityName', optionsValue: 'CityId', optionsCaption: 'Choose...'"></select>
</p>
<script type='text/javascript'>
function CascadingDDLViewModel() {
this.states = ko.observableArray([]);
this.cities = ko.observableArray([]);
}
var objVM = new CascadingDDLViewModel();
ko.applyBindings(objVM);
function FetchStates() {
var countryCode = $("#ddlCountry").val();
$.getJSON("/Home/GetStates/" + countryCode, null, function (data) {
objVM.states(data);
});
function FetchCities() {
var stateId = $("#ddlStates").val();
$.getJSON("/Home/GetCities/" + stateId, null, function (data) {
objVM.cities(data);
});
}
</script>
<p>
<b>Select Country :</b> @Html.DropDownList("ddlCountry", ViewBag.Country as SelectList,"Select...", new { onchange = "FetchStates();" })
</p>
<p data-bind="visible: states().length > 0">
<b>Select State :</b> <select id="ddlStates" data-bind="value: selectedState,options: states, optionsText: 'StateName', optionsValue: 'StateId', optionsCaption: 'Choose...'"></select>
</p>
<p data-bind="visible: cities().length > 0">
<b>Select City :</b> <select data-bind="options: cities, optionsText: 'CityName', optionsValue: 'CityId', optionsCaption: 'Choose...'"></select>
</p>
<script type='text/javascript'>
function CascadingDDLViewModel() {
this.states = ko.observableArray([]);
this.cities = ko.observableArray([]);
this.selectedState = ko.observable();
}
var objVM = new CascadingDDLViewModel();
objVM.selectedResource.subscribe(function (stateId) {
$.getJSON("/Home/GetCities/" + stateId, null, function (data) {
objVM.cities(data);
});
});
ko.applyBindings(objVM);
function FetchStates() {
var countryCode = $("#ddlCountry").val();
$.getJSON("/Home/GetStates/" + countryCode, null, function (data) {
objVM.states(data);
objVM.cities.removeAll();
});
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.