1
我是新淘汰赛和asp.net mvc两个。 我想插入更新删除数据与数据库中的淘汰赛。我的淘汰赛模式是不能保存数据第二次淘汰赛mvc
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.CityId = ko.observable();
self.CityName = ko.observable();
self.selectedCity = ko.observable();
// self.City = ko.observable();
selectCity = function (item) {
self.selectedCity(item);
}
//load
loadCitys = function() {
$.getJSON("/Admin/GetCitys", {}, function (result) {
var mappedCitys = ko.utils.arrayMap(result.Data, function (item) {
return new City(item);
});
self.Citys([]);
self.Citys.push.apply(self.Citys, mappedCitys);
});
}
//edit
EditCity = function (item) {
//what need to do here
// is it possible to fill the hidden fild and the text box ??
}
//save
SaveCity = function (item) {
City = new City(item);
$.ajax({
type: "POST",
url: "/Admin/SaveCity",
data: ko.toJSON({ City: City }),
contentType: "application/json",
success: function (result) {
if (result.Edit) {
City.CityId = result.Success;
City.CityName = item.CityName;
self.Citys.push(City);
toastr.success('City Information Save Successfully', 'Success');
}
else if (result.Edit == false) {
toastr.success('City Information Update Successfully', 'Success');
}
else {
toastr.error('There is an error please try again later', 'Errror');
}
}
});
}
//delete
DeleteCity = function (City) {
$.ajax("/Admin/DeleteCity", {
data: ko.toJSON({ CityId: City.CityId }),
type: "POST", contentType: "application/json",
success: function (result) {
if (result.Success) {
self.Citys.remove(City);
toastr.success('City Remove Successfully', 'Success');
}
else {
alert("Error..");
}
}
});
}
}
(function() {
ko.applyBindings(new CityViewModel, document.getElementById("Citys"));
loadCitys();
});
而且我的HTML代码
<table class="table table-striped">
<thead>
<tr>
<th>City Id</th>
<th>City Name</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody data-bind="foreach: $root.Citys">
<tr data-bind="click: selectCity">
<td><span data-bind="text:CityId"></span></td>
<td><span data-bind="text:CityName"></span></td>
<td><button data-bind="click: EditCity" class="btn btn-primary">Edit</button></td>
<td><button data-bind="click: DeleteCity" class="btn btn-danger">Delete</button></td>
</tr>
</tbody>
</table>
<fieldset>
<legend>Add new/Edit City</legend>
<label>City name</label>
<input type="hidden" data-bind="value: CityId" />
<input type="text" data-bind="value: CityName" placeholder="Type city name…">
<button type="submit" data-bind="click: SaveCity" class="btn">Submit</button>
</fieldset>
有了这个代码,我可以得到的数据形式的数据库在我看来,成功将文件显示它们, 我从数据库中删除数据,和我也可以插入数据到数据库,但这里是一个问题,我可以保存数据只有当我改变文本框的值(无页面刷新),并尝试保存城市信息,然后它说(在我的JavaScript代码的Firebug):
TypeError:城市不是构造函数 City = new City(item);
我的问题是我在这段代码中做了什么错误,我试图在编辑按钮单击时填充文本框和隐藏字段,我该怎么做? 在此先感谢。
感谢您抽出宝贵的时间来写一个完整的例子! – neontapir
感谢您的大力帮助,这很好,我在这里更改var city = self.EditingCity(ko.toJSON(item));外面它发布空值 – Ronjon
不,它应该工作我的方式,只要你的'数据'行在ajax文章是“data:ko.toJSON({City:city})”。请记住,您的HTML也必须更新,才能以我的方式工作。 – RodneyTrotter