我在后端有一个使用 WebMethods 的 Kendo-UI 网格。我的两个功能,读取和创建,工作得非常好。Create 方法接受我的对象并返回相同类型的对象,并且一切正常。问题是更新方法拒绝接受完全相同的东西!
这是我的两个 WebMethods,您可以看到它们在返回类型和输入方面基本相同......
[WebMethod()]
public static DISP_PROJECT_NAME_MASTER createProject(DISP_PROJECT_NAME_MASTER project) {
using (DisputeProjectManagerEntities ctx = new DisputeProjectManagerEntities()) {
project.MODIFIED_USER = "user.name"; //TODO: Use Actual Security context
ctx.AddToDISP_PROJECT_NAME_MASTER(project);
ctx.SaveChanges();
}
return project;
}
[WebMethod()]
public static DISP_PROJECT_NAME_MASTER updateProject(DISP_PROJECT_NAME_MASTER project) {
DISP_PROJECT_NAME_MASTER pUpdate = null;
using (DisputeProjectManagerEntities ctx = new DisputeProjectManagerEntities()) {
pUpdate = (DISP_PROJECT_NAME_MASTER)(from p in ctx.DISP_PROJECT_NAME_MASTER where p.PROJECT_ID_NUMBER == project.PROJECT_ID_NUMBER select p).First();
//copy the fields, I guess...
pUpdate.ACTIVE = project.ACTIVE;
pUpdate.CATEGORY = project.CATEGORY;
pUpdate.CONTRIBUTION = project.CONTRIBUTION;
pUpdate.DESCRIPTION = project.DESCRIPTION;
pUpdate.JUSTIFICATION = project.JUSTIFICATION;
pUpdate.MODIFIED_DATE = DateTime.Now;
pUpdate.MODIFIED_USER = "user.name"; //TODO: Use Actual Security context
pUpdate.PROJECT_NAME = project.PROJECT_NAME;
pUpdate.SETTLEMENT = project.SETTLEMENT;
pUpdate.WIN_RATE = project.WIN_RATE;
ctx.SaveChanges();
}
return pUpdate;
}
这是我的剑道网格代码...
$(document).ready(function () {
setupGrid();
});
function setupGrid() {
var myDataSource = new kendo.data.DataSource({
type: "odata",
pageSize: 10,
serverPaging: false,
serverFiltering: false,
serverSorting: false,
data: "d",
transport: {
read: {
type: "POST",
url: "/special_pages/DisputeProjectManager.aspx/getProjects",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{}",
success: function (response) { alert('WORKED!'); },
error: function (xhr, textStatus, errorThrown) { handleAjaxError(xhr, textStatus, errorThrown); }
},
update: {
type: "POST",
url: "/special_pages/DisputeProjectManager.aspx/updateProject",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: function (data) { return { project: data }; },
success: function (response) { alert('Project Updated'); },
error: function (xhr, textStatus, errorThrown) { handleAjaxError(xhr, textStatus, errorThrown); }
},
create: {
type: "POST",
url: "/special_pages/DisputeProjectManager.aspx/createProject",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: function (data) { return { project: data }; },
success: function (response) { alert('Project Created'); },
error: function (xhr, textStatus, errorThrown) { handleAjaxError(xhr, textStatus, errorThrown); }
},
parameterMap: function (data, type) {
return kendo.stringify(data);
}
},
schema: {
data: "d",
total: "d.length",
model: {
id: "PROJECT_ID_NUMBER",
fields: {
"PROJECT_ID_NUMBER": { type: "number", editable: false },
"PROJECT_NAME": { type: "string", editable: true },
"CATEGORY": { type: "string", editable: true },
"CONTRIBUTION": { type: "string", editable: true },
"WIN_RATE": { type: "string", editable: true },
"SETTLEMENT": { type: "string", editable: true },
"DESCRIPTION": { type: "string", editable: true },
"JUSTIFICATION": { type: "string", editable: true },
"ACTIVE": { type: "string", editable: true },
"MODIFIED_USER": { type: "string", editable: false },
"MODIFIED_DATE": { type: "date", editable: false }
}
}
},
batch: false
});
$("#projectManagerGrid").kendoGrid({
dataSource: myDataSource,
columns: [{ command: "edit", title: "", width: 110 },
{ field: "PROJECT_NAME", title: "Name", width: 200 },
{ field: "CATEGORY", title: "Category", width: 150 },
{ field: "CONTRIBUTION", title: "Contribution", width: 120 },
{ field: "WIN_RATE", title: "Win Rate", width: 90 },
{ field: "SETTLEMENT", title: "Settlement", width: 100 },
{ field: "DESCRIPTION", title: "Description", width: 300 },
{ field: "JUSTIFICATION", title: "Justification", width: 100 },
{ field: "ACTIVE", title: "Active Flag", width: 100 },
{ field: "MODIFIED_USER", title: "Last Mod User", width: 150 },
{ field: "MODIFIED_DATE", title: "Last Mod Date", format: "{0:MMMM dd, yyyy}", width: 150 }
],
editable: "inline",
toolbar: ["create", "save", "cancel"],
pageable: true,
sortable: true,
resizable: true
});
}
function handleAjaxError(xhr, textStatus, errorThrown) {
alert(errorThrown);
}
现在,当我调用 Create 函数时,它工作正常。当我调用 Update 函数时,出现常见错误(我的函数从未运行,并且此响应出现 500 错误):无法将 DISP_PROJECT_NAME_MASTER 转换为 IDictionary
使用此堆栈跟踪:
Cannot convert object of type \u0027DisputeProjectManagerModel.DISP_PROJECT_NAME_MASTER\u0027 to type \u0027System.Collections.Generic.IDictionary`2[System.String,System.Object]\u0027
at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n
at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n
at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n
at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n
at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n
at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n
at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)
ExceptionType:System.InvalidOperationException
我无法摆脱那个愚蠢的错误,即使我将我的 WebMethod 更改为此......
public static void updateProject(object project) {
真正令人抓狂的是,我仍然得到与该方法签名相同的错误,并且没有涉及 DISP_PROJECT_NAME_MASTER 对象!它到底在哪里得到那个对象,为什么它在没有理由的情况下试图转换为 IDictionary?!
使用上面的代码,这就是请求中发布的内容......
{
"project": {
"__type": "DisputeProjectManagerModel.DISP_PROJECT_NAME_MASTER",
"PROJECT_ID_NUMBER": 483,
"PROJECT_NAME": "Updated Name",
"CATEGORY": "UPDATED Category",
"CONTRIBUTION": "N",
"WIN_RATE": "N",
"SETTLEMENT": "N",
"DESCRIPTION": "Desc",
"JUSTIFICATION": "Just",
"ACTIVE": "N",
"MODIFIED_USER": "user.name",
"MODIFIED_DATE": "2013-09-03T21:20:13.000Z",
"EntityState": 2,
"EntityKey": {
"EntitySetName": "DISP_PROJECT_NAME_MASTER",
"EntityContainerName": "DisputeProjectManagerEntities",
"EntityKeyValues": [
{
"Key": "PROJECT_ID_NUMBER",
"Value": 483
}
],
"IsTemporary": false
}
},
"__type": "DisputeProjectManagerModel.DISP_PROJECT_NAME_MASTER",
"PROJECT_ID_NUMBER": 483,
"PROJECT_NAME": "Updated Name",
"CATEGORY": "UPDATED Category",
"CONTRIBUTION": "N",
"WIN_RATE": "N",
"SETTLEMENT": "N",
"DESCRIPTION": "Desc",
"JUSTIFICATION": "Just",
"ACTIVE": "N",
"MODIFIED_USER": "user.nam",
"MODIFIED_DATE": "2013-09-03T21:20:13.000Z",
"EntityState": 2,
"EntityKey": {
"EntitySetName": "DISP_PROJECT_NAME_MASTER",
"EntityContainerName": "DisputeProjectManagerEntities",
"EntityKeyValues": [
{
"Key": "PROJECT_ID_NUMBER",
"Value": 483
}
],
"IsTemporary": false
}
}
有人请解释一下,我不只是在寻找代码修复,我需要知道这里发生了什么。我已经阅读了整个 Encosia 网站,以及这里的 40-50 个问题,除了那些不起作用的东西之外,没有关于如何解决这个问题的解释。这是怎么回事?
第二个问题是为什么我的回调没有运行?使用此功能时,我从未见过弹出警报-如果它们不起作用,回调有什么用?
我对剑道这件事感到非常沮丧——我开始后悔为此付出了真金白银。我已经为此工作了三天,如果我使用的是老式的数据网格,我现在就完成了。