javascript - kendo grid发送json到 Controller

标签 javascript .net kendo-grid

当我删除一行时,我从剑道网格发送一个 json,但在 Controller 中我收到 null。

$(document).ready(function () {
                $("#grid").kendoGrid({
                    dataSource: {
                        type: "json",
                        transport: {
                            read: {
                                type: "POST",
                                dataType: "json",
                                url: "/Home/GetPatients"
                            },
                            destroy: {
                                url: '@Url.Action("deletePatient", "Home")',
                                type: "POST",
                                contentType: "application/json; charset=utf-8",
                                dataType: "json"
                            },
                            parameterMap: function (options, operation) {
                                if (operation !== "read" && options)
                                    return kendo.stringify(options);
                            }
                        },
                        schema: {
                            model: {
                                id: "Id",
                                fields: {
                                    FirstName: { type: "string" },
                                    LastName: { type: "string" },
                                    CNP: { type: "string" },
                                    Birthdate: { type: "date" }
                                }
                            }
                        },
                        pageSize: 20,
                    },
                    height: 300,
                    sortable: true,
                    pageable: {
                        refresh: true,
                        pageSizes: true,
                        buttonCount: 5
                    },
                    toolbar: ["create"],
                    columns:
                        [
                            { field: "LastName", title: "Nume" },
                            { field: "FirstName", title: "Prenume" },
                            { field: "CNP", title: "CNP" },
                            { field: "Birthdate", title: "Data nasterii", format: "{0:dd/MM/yyyy}" },
                            {
                                command:
                                    [
                                        {
                                            name: "edit"
                                        },
                                        {
                                            name: "destroy",
                                        }
                                    ],
                                    title: "", width: "172px"
                            }
                        ],
                    editable: "popup"
                });
            });

我将debug放在方法中,实际参数'j'为空,它应该是一个字符串(序列化json)。 在 chrome -> Debug模式 -> 网络 -> 我得到状态 200 ok,在请求负载中我查看了包含所有行数据的 json,事实是没问题,但我被卡住了。

[HttpPost]
public ActionResult deletePatient(string j)
{
    //List<Patient> objName = new JavaScriptSerializer().Deserialize<List<Patient>>(j);
    int a = 5;
    a = a - 4;
    return Json(a);
}

最佳答案

您正在发送一个字符串并假装它是 JSON(请参阅您的 contentTypedata)。如果您将其更改为类似 text/plain 的内容,它可能会起作用。

但你根本不应该尝试这个!您应该在 C# 中创建一个 PatientModel,它具有 FirstNameLastName 等属性,并在您的 Controller 方法中期待这个模型。这样您就不必自己序列化和反序列化数据。考虑到您已经有一个操作 GetPatients,我猜您已经有了该模型。使用它。

关于javascript - kendo grid发送json到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55489764/

相关文章:

javascript - 将我们禁用的kendo ui 网格列设为灰色

javascript - 显式触发剑道网格的自定义命令

JavaScript 无法选择复选框

javascript - 有分钟吗jquery datepicker javascript 文件的版本

javascript - require()函数的Node.js源码

c# - 如何对 WCF 项目进行集成测试?

c# - 在 Visual Studio 调试器中打印漂亮的对象图

javascript - 使用内联编辑和自定义编辑器下拉控件的 Kendo Grid

JavaScript 使用 bool 来控制回调

c# - .Net 项目的 TeamCity 增量测试