0
使用基因敲除我試圖將3個下拉列表與由調用WebAPI的Ajax方法返回的數據綁定。使用包含3 IList的JSON對象的3個下拉列表的基因敲除映射<CustomType>
接收到的JSON數據是類型,其中calsses定義如下
public class MasterItem
{
public int MasterItemID { get; set; }
public string MasterItemName { get; set; }
}
public class ReconDefMasters
{
public IList<MasterItem> servFileFormat { get; set; }
public IList<MasterItem> mersRev { get; set; }
public IList<MasterItem> mersFileType { get; set; }
}
如何寫這個ko.mapping「ReconDefMasters」?
我能夠通過返回IList來實現結果,但它需要3次獨立的WebAPI調用,並試圖通過返回3個IList的組合對象來避免這種多次調用。
我稱爲下面的鏈接,但在修改邏輯3下拉列表一起
http://jsfiddle.net/jearles/CGh9b/
Poblem with getting multidimensional array (object) observable in KnockoutJS
不成功這是我單的IList <代碼>方法
var apipath = 'http://example.com';
ko.validation.init({
registerExtenders: true,
messagesOnModified: true,
insertMessages: false
});
function Master(data) {
var self = this;
self.FormatId = ko.observable(data.MasterItemID);
self.FormatName = ko.observable(data.MasterItemName);
}
var RuleDefVM = function (url) {
var self = this;
self.ServFileFormat = ko.observableArray();
self.selectedTemplate = ko.observable();
self.RuleDef = function() {
jQuery.support.cors = true;
$.ajax({
type: "GET",
url: url,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
if (response != "") {
$(response).each(function (index, element) {
self.ServFileFormat.push(new Master(element));
});
}
}
});
};
}
var urlRuleDef = apipath + '/api/RuleDef/';
var viewModelRuleDef = new RuleDefVM(urlRuleDef);
viewModelRuleDef.RuleDef();
A您是否問過如何將您的'ReconDefMasters'對象序列化爲JSON,或者如何處理客戶端上的JSON或兩者? – 2014-11-25 11:36:56
如何處理客戶端上的JSON - 將Knockout綁定到3個下拉列表 – user3122606 2014-11-25 12:21:49
您的模型需要有3個'observableArrays',每個'List'一個 - 然後您可以將它們單獨綁定到您的模板等 – 2014-11-25 12:50:54