将模型传递给控制器​​时 Kendo 网格数据源上的错误 "Invalid JSON primitive: models."

Error "Invalid JSON primitive: models." on Kendo Grid datasource while passing model to controller

实施 Kendo 网格内联编辑 CRUD 时出现错误。当我添加新记录并更新它时,我收到错误的响应代码 500:

我认为问题出在 parameterMap。在 Kendo 中将模型传递给控制器​​的正确方法是什么?

Invalid JSON primitive: models.

有哪些型号?

源代码:

$(document).ready(function () {
        var baseUrl = "/SomeUrl",
            dataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                        url: baseUrl + "/GetAccolades?profileId=" + @profileId,
                        type: "GET",
                        dataType: "json"
                    },
                    create: {
                        url: baseUrl + "/AddAccolade",
                        type: "POST",
                        dataType: "json",
                        contentType: "application/json",
                    },
                    update: {
                        url: baseUrl + "/UpdateAccolade",
                        type: "PUT",
                        dataType: "json",
                        contentType: "application/json",
                    },
                    delete: {
                        url: baseUrl + "/DeleteAccolade",
                        type: "DELETE",
                        dataType: "json"
                    },
                    parameterMap: function (options, operation) {
                        if (operation !== "read" && options.models) {
                            alert(kendo.stringify(options.models));
                            return { models: kendo.stringify(options.models) };
                        }
                    }
                },
                batch: true,
                schema: {
                    model: {
                        id: "ProfileAccoladeID",
                        fields: {
                            ProfileAccoladeID: { editable: false, nullable: false },
                            ProfileID: { editable: false, nullable: false, defaultValue: @profileId  },
                            Years: { type: "string" },
                            Name: { type: "string" },
                            Level: { type: "string" },
                            Event: { type: "string" },                              
                        }
                    }
                }
            });

        $("#accolades-grid").kendoGrid({
            dataSource: dataSource,
            pageable: false,
            height: 550,
            toolbar: ["create"],
            columns: [
                { field: "Years", width: "150px" },
                { field: "Name", title: "Name", width: "150px" },
                { field: "Level", title: "Level", width: "150px" },
                { field: "Event", title: "Event", width: "450px" },                    
                { command: ["edit", "destroy"], title: " ", width: "250px" }],
            editable: "inline"
        });
    });
</script>

控制器方法:

    [HttpPost]
    public JsonResult AddAccolade(ProfileAccolade accolade)
    {
        using (var db = new XXXEntities())
        {
            if (accolade != null)
            {
                var newAccolade = new ProfileAccolade()
                {
                    ProfileID = accolade.ProfileID,
                    Years = accolade.Years,
                    Name = accolade.Name,
                    Level = accolade.Level,
                    Event = accolade.Event
                };
                db.ProfileAccolades.Add(newAccolade);
                db.SaveChanges();

                return Json(new { Success = true });
            }
            else
            {
                return Json(new { Success = false, Message = "Error occured" });
            }
        }
    }

如何修复此错误?

更新:

通过删除 contentType: "application/json",,错误 Invalid JSON primitive: models. 消失了。但是,控制器没有得到模型。

有解决此问题的想法吗?

问题源于在 kendo.data.DataSourcetransport.createtransport.update 部分使用 contentType: "application/json"。由于这两个操作都使用 AJAX 调用(即 jQuery.ajax() 方法),contentType 设置会影响请求正文的发送方式。

通过设置"application/json"内容类型,请求体将被视为JSON内容,但实际上transport.parameterMapreturnsURL编码版本包含models 并将 JSON 字符串格式化为键值对 (KVP)。这就是为什么会出现 "Invalid JSON primitive" 错误,因为 URL 编码格式与 JSON 格式不同。

如果要为AJAX调用保留"application/json"设置,添加JSON.stringify方法将models的URL编码格式转换为JSON数据:

parameterMap: function (options, operation) {
    if (operation !== "read" && options.models) {
        return JSON.stringify({ models: kendo.stringify(options.models) });
    }
}

但是,如果您想将 parameterMap 作为默认内容类型 (application/x-www-form-urlencoded) 发送,只需删除所有 contentType 定义以将其设置回去:

dataSource = new kendo.data.DataSource({
     transport: {
         read: {
             url: baseUrl + "/GetAccolades?profileId=" + @profileId,
             type: "GET",
             dataType: "json"
         },
         create: {
             url: baseUrl + "/AddAccolade",
             type: "POST",
             dataType: "json"
         },
         update: {
             url: baseUrl + "/UpdateAccolade",
             type: "PUT",
             dataType: "json"
         },
         delete: {
             url: baseUrl + "/DeleteAccolade",
             type: "DELETE",
             dataType: "json"
         },
         parameterMap: function (options, operation) {
             if (operation !== "read" && options.models) {
                 alert(kendo.stringify(options.models));
                 return { models: kendo.stringify(options.models) };
             }
         }
     },
     // other DataSource definitions
});

参考:

kendo.data.DataSource (Kendo UI Documentation)