且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

可选参数必须是引用类型,可空类型,或者声明为可选参数。

更新时间:2022-06-09 01:34:03

.ajax( {

url:'Product / GetAllProducts',

cache:false,

类型:'GET',

contentType:'application / json; charset = u tf-8',

数据:{},

成功:函数(数据){

self.Products(data); //将响应放入ObervableArray

}

});



//初始化后计算总价

self.Total = ko.computed(function(){

var sum = 0;

var arr = self.Products();

for(var i = 0; i< arr.length; i ++){

sum + = arr [i] .Price;

}

返还金额;

});



//添加新商品

self.create = function(){

if(Product.Name()!=&& Product.Price()!=&&& Product.Category ()!=){
.ajax({
url: 'Product/GetAllProducts',
cache: false,
type: 'GET',
contentType: 'application/json; charset=utf-8',
data: {},
success: function (data) {
self.Products(data); // Put the response in ObervableArray
}
});

// Calculate total Price after initialization
self.Total = ko.computed(function () {
var sum = 0;
var arr = self.Products();
for (var i = 0; i < arr.length; i++) {
sum += arr[i].Price;
}
return sum;
});

// Add New Item
self.create = function () {
if (Product.Name() != "" && Product.Price() != "" && Product.Category() != "") {


.ajax({

url:'Product / AddProduct',

cache:false,

类型:'POST',

contentType:'application / json; charset = utf-8',

data :ko.toJSON(产品),

成功:功能(数据){

self.Products.push(数据);

self.Name();

self。价格();

self.Category();

alert('产品已成功添加!');

}

})。失败(

函数(xhr,textStatus,错误){

alert(错误);

});

}

else {

alert('EMPLY FIELDS不允许'------- -------------------------------------------------- ----- \ n请输入所有值。\ n所有字段都是必填项。');

}

}


//删除产品详细信息

self.delete = function(产品){

if(确认('你确定要删除''+ Product.Name +'product ??')) {

var id = Product.Id;


.ajax({
url: 'Product/AddProduct',
cache: false,
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: ko.toJSON(Product),
success: function (data) {
self.Products.push(data);
self.Name("");
self.Price("");
self.Category("");
alert('Product has been added successfully!');
}
}).fail(
function (xhr, textStatus, err) {
alert(err);
});
}
else {
alert('EMPLY FIELDS ARE NOT ALLOWED\n--------------------------------------------------------------\nPlease enter all the values.\nAll fields are required.');
}
}

// Delete Product Details
self.delete = function (Product) {
if (confirm('Are you sure to delete"' + Product.Name + '" product ??')) {
var id = Product.Id;


.ajax({

url:'Product / DeleteProduct /'+ id,

cache:false,

类型:'POST',

contentType:'application / JSON; charset = utf-8',

数据:id,

成功:函数(数据){

self.Products.remove(Product) ;

alert('产品已被删除!');

}

})。失败(

function(xhr,textStatus,err){

self.status(err);

});

}

}



//编辑产品详情

self.edit = function(产品){

self .Product(产品);

}



//更新产品详情

self.update = function( ){

var Product = self.Product();

var id = Product.Id;

console.log(id);

if(Product.Name!=& Product.Price!=& Product.Category!=){
.ajax({
url: 'Product/DeleteProduct/' + id,
cache: false,
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: id,
success: function (data) {
self.Products.remove(Product);
alert('Product has been deleted!');
}
}).fail(
function (xhr, textStatus, err) {
self.status(err);
});
}
}

// Edit Product Details
self.edit = function (Product) {
self.Product(Product);
}

// Update Product Details
self.update = function () {
var Product = self.Product();
var id = Product.Id;
console.log(id);
if (Product.Name != "" & Product.Price != "" & Product.Category != "") {