我在這裏抨擊我的頭靠在牆上,試圖弄清楚爲什麼我無法使它工作。我已經嘗試了以下很多例子,並將我所嘗試的所有內容都剝離到了我的下面。使用ASP.NET Webforms KnockoutJS
所以,我的aspx頁面上,我有:
<input type="radio" data-bind="value: individual" />Individual
這裏是我的javascript:
var serviceBase = 'http://localhost:49906/PopulationSelection.aspx/';
var getSvcUrl = function (method) { return serviceBase + method; };
var ajaxGetJson = function (method, jsonIn, callback) {
$.ajax({
url: getSvcUrl(method),
type: "GET",
data: ko.toJSON(jsonIn),
dataType: "json",
contentType: "application/json",
success: function (json) {
callback(json.d);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr + ' ' + thrownError);
}
});
}
var batchesDataService = {
getSavedBatches: function (callback) {
ajaxGetJson("GetBatches", null, callback);
}
};
var Batch = function (p) {
this.individual = ko.observable(p.Individual);
this.household = ko.observable(p.Household);
this.countOnly = ko.observable(p.CountOnly);
this.femalePrimary = ko.observable(p.FemalePrimary);
this.eventManagement = ko.observable(p.EventManagement);
this.eventManagementText = ko.observable(p.EventManagementText);
this.randomSampling = ko.observable(p.RandomSampling);
this.randomSamplingText = ko.observable(p.RandomSamplingText);
this.stateHasChanged = ko.observable(false);
};
var loadBatchesCallback = function (data) {
var parsed = JSON.parse(data);
myViewModel.Batch = new Batch(parsed);
//also tried:
//myViewModel.Batch(new Batch(parsed));
};
var myViewModel;
var viewModel = function() {
this.Batch = ko.observable();
this.getBatchInfo = function() {
batchesDataService.getSavedBatches(loadBatchesCallback);
};
};
$(document).ready(function() {
myViewModel = new viewModel();
myViewModel.getBatchInfo();
ko.applyBindings(myViewModel.Batch);
});
我有從我的webmethod獲取數據回沒問題(使用會話訪問),當我提醒批處理成員時,我會看到正確的信息。
我的問題來自實際ko.applyBindings()
。無論我嘗試了什麼,我在控制檯中出現以下錯誤:
Uncaught Error: Unable to parse bindings.
Message: ReferenceError: individual is not defined;
Bindings value: value: individual
任何幫助表示讚賞!
這樣做。我改變了loadBatchesCallback,如下所示: 'var loadBatchesCallback = function(data){ var parsed = JSON.parse(data); myViewModel.Batch = new Batch(parsed); ko.applyBindings(myViewModel.Batch); };' – JCompo
我忘了說謝謝! – JCompo