我有一个kendo网格,它使用了kendo dataSource,我不习惯使用kendo dataSource来进行内联编辑,通常我不会进行内联编辑,但我被要求这样做。
我的dataSource如下
var text = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}';
var obj = JSON.parse(text);
alert(obj);
alert(obj.employees[0].firstName);
alert(obj['employees'][0].firstName);
用于传输更新的控制器方法是
function PriceLookupGridDataSource() {
var dataSource = new kendo.data.DataSource({
transport: {
read: {
url: "../KendoGridTesting/GetThePriceLookupGrid",
dataType: "json"
},
update: {
url: function(item) {
console.log(item);
return "../KendoGridTesting/PassAnObject?myObj=" + item;
}
}
},
schema: {
model: {
id: "MaterialTypeID",
fields: {
VendorID: { type: "number" },
VendorName: { type: "string" },
Description: { type: "string" },
MaterialTypeID: { type: "number" },
MaterialType: { type: "string" },
ServicePrice: { type: "string" },
SellUOM: { type: "string" },
Cost: { type: "string" },
PurchaseUOM: { type: "string" }
}
}
},
batch: false,
pageSize: 20
});
return dataSource;
}
对象是
对象{VendorID:26,VendorName:" ACME STONE MASONRY",描述:" Special Stone Quote",MaterialTypeID:35,MaterialType:" Cladding" ... }
并且PriceLookupGrid数据类是
[HttpPost]
public void PassAnObject(PriceLookupGrid myObj)
{
...
}
当我尝试将其传递给我的控制器时,我收到错误
GET http://localhost:51193/KendoGridTesting/PassAnObject?id=[object%20Object]&V ... .0000& ServicePrice = 0.0000 ++++& SellUOM =每个& Cost = 0.0000& PurchaseUOM = EachBBB 404(未找到)
传递"项目"作为单独的参数,我不希望发生这种情况。
任何想法?
答案 0 :(得分:1)
您的操作将按照[HttpPost]
进行修饰,但您的错误消息表明您正在执行GET
。您需要Kendo POST
数据,而不是:
update: {
url: "../KendoGridTesting/PassAnObject",
type: "post"
}
答案 1 :(得分:0)
这是因为item
是一个对象,您可以在控制台中看到。你不能只是在url字符串中连接它。你要解析它。它有一个名为models
的属性,其中所有已更改(或脏)的行将被发送到服务器。
为了让ASP.Net MVC将您的数据绑定到您的模型中,您必须发送如下日期:
../KendoGridTesting/PassAnObject?VendorID=1&VendorName=...
将数据“解析”的简单方法是:
var model = {VendorID: 26, VendorName: "ACME STONE MASONRY", Description: "Special Stone Quote", MaterialTypeID: 35, MaterialType: "Cladding"};
function modelToUrlParams(model) {
var params = [];
Object.keys(model).forEach(key => params.push(key + "=" + model[key]));
return encodeURI("?" + params.join("&"));
};
var myUrl = "../KendoGridTesting/PassAnObject" + modelToUrlParams(model);
console.log("Final Url", myUrl);