После вставки новой строки в сетку кендо при обновлении определенной строки сетка кендо вызывает метод create (insert) вместо метода обновления. Пожалуйста, помогите мне. Я также предоставил для справки метод создания и метод обновления. Я не могу понять, как это делать. Пожалуйста, помогите.
<script>
function LoadCouponCodeGrid() {
$("#coupon-code-grid").kendoGrid({
dataSource: {
transport: {
read: "/Data/GetCouponCode",
update: {
url: "/Data/UpdateCouponCode",
type: "POST",
dataType: 'json',
contentType: "application/json"
},
destroy: {
url: "/Data/DestroyCouponCode",
type: "POST",
dataType: 'json',
contentType: "application/json"
},
create: {
url: "/Data/CreateCouponCode",
type: "POST",
dataType: 'json',
contentType: "application/json"
//success: function () {
// setTimeout(location.reload.bind(location), 5000);
//}
},
parameterMap: function (options, operation) {
if (operation !== "read" && options.models) {
var values = {};
values["CodeId"] = options.models[0].CodeId;
values["CodeName"] = options.models[0].CodeName;
values["ValidFrom"] = options.models[0].ValidFrom;
values["ValidTo"] = options.models[0].ValidTo;
values["Amount"] = options.models[0].Amount;
return JSON.stringify(values);
}
}
},
batch: true,
pageSize: 15,
schema: {
model: {
id: "CodeId",
fields: {
CodeId: { editable: false, nullable: true },
CodeName: { type: "string", validation: { required: true, pattern: { value:
"^[A-Z0-9]*$"} } },
ValidFrom: { type: "date", validation: { required: true } },
ValidTo: { type: "date", validation: { required: true } },
Amount: { type: "number", validation: { required: true, min: 1 } }
}
}
},
},
toolbar: ["create"],
pageable: true,
scrollable: true,
columns: [
{
field: "CodeId",
title: "Code Id",
hidden: true
},
{
field: "CodeName",
width: 120,
title: "Coupon Code"
}, {
field: "ValidFrom",
width: 140,
title: "Valid From", type: "date", format: "{0:yyyy-MM-dd}"
}, {
field: "ValidTo",
width: 140,
title: "Valid To", type: "date", format: "{0:yyyy-MM-dd}"
}, {
field: "Amount",
width: 110,
title: "Amount", format: "{0:c}"
},
{ command: ["edit", "destroy"], title: " ", width: "170px" }],
editable: "inline"
});
}
$(document).ready(function () {
LoadCouponCodeGrid();
});
</script>
@section scripts{
<script>
</script>
}
<script src="https://kendo.cdn.telerik.com/2020.2.617/js/kendo.all.min.js">
</script>
Это мой метод обновления:
public CouponCodeModel UpdateCouponCode(CouponCodeModel couponCode)
{
DateTime ValidFrom = couponCode.ValidFrom;
DateTime ValidTo = couponCode.ValidTo;
string query = @"
declare @CodeId int = " + couponCode.CodeId + @"
, @CodeName nvarchar(20) = '" + couponCode.CodeName.Replace("'", "''")
+ @"'
, @ValidFrom datetime = '" + ValidFrom.ToString("yyyy-MM-dd") + @"'
, @ValidTo datetime = '" + ValidTo.ToString("yyyy-MM-dd") + @"'
, @Amount money = " + couponCode.Amount + @"
update CouponCodes set
CodeName=@CodeName,ValidFrom=@ValidFrom,ValidTo=@ValidTo,Amount=@Amount
where CodeId = @CodeId";
return db.Database.SqlQuery<CouponCodeModel>(query).FirstOrDefault();
}
И мой метод вставки (создания)
public CouponCodeModel CreateCouponCode(CouponCodeModel couponCode)
{
DateTime ValidFrom = couponCode.ValidFrom;
DateTime ValidTo = couponCode.ValidTo;
string query = @"
declare
@CodeName nvarchar(20) = '" + couponCode.CodeName.Replace("'" ,"''") +
@"'
, @ValidFrom datetime = '" + ValidFrom.ToString("yyyy-MM-dd") + @"'
, @ValidTo datetime = '" + ValidTo.ToString("yyyy-MM-dd") + @"'
, @Amount money = " + couponCode.Amount + @"
insert into CouponCodes(CodeName,ValidFrom,ValidTo,Amount)
values( @CodeName, @ValidFrom, @ValidTo, @Amount)";
return db.Database.SqlQuery<CouponCodeModel>(query).FirstOrDefault();
}