我有这个用于插入,更新,删除和显示城市的淘汰视图模型
function City(data) {
this.CityId = ko.observable(data.CityId);
this.CityName = ko.observable(data.CityName);
}
function CityViewModel() {
var self = this;
self.Citys = ko.observableArray([]);
self.SelectedCity = ko.observable();
self.EditingCity = ko.observable(new City({ CityId: '', CityName: '' }));
self.EditCity = function (city) {
$.ajax("/Controller/GetSingalCity/" + ko.toJSON(city.CityId), {
data: ko.toJSON({ CityId: city.CityId }),
type: "POST", contentType: "application/json",
success: function (result) {
if (result.Success) {
var cityid = result.Data.CityId;
var cityname = result.Data.CityName;
self.EditingCity(new City({ CityId: cityid, CityName: cityname }));
}
else {
alert("Error..");
}
}
});
};
}
每次插入更新删除时都使用此代码我必须使用
self.EditingCity(new City({ CityId: cityid, CityName: cityname }));
或
self.EditingCity(new City({ CityId: '', CityName: '' }));
我不想创造自我.EditingCity(新城市({CityId:'',CityName:''}));对于每个操作(插入,更新,删除)。任何人都可以告诉我如何? 我是淘汰赛的初学者....
答案 0 :(得分:1)
您无需创建新的City对象。相反,只需更新现有的。 1}个对象的数据绑定视图应该自动更新。
所以,而不是写作:
EditingCity
你应该写:
self.EditingCity(new City({ CityId: cityid, CityName: cityname }));