簡體   English   中英

kendo網格將json發送到控制器

[英]kendo grid send json to controller

刪除行時,我從kendo網格發送json,但在控制器中我收到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'為null,它應該是一個字符串(序列化json)。 在chrome->調試模式->網絡->我的狀態為200,在請求有效負載中,我查看了所有行數據的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#中創建一個具有FirstNameLastName等屬性的PatientModel ,並在控制器方法中使用此模型。 這樣,您不必自己對數據進行序列化和反序列化。 考慮到您已經有一個動作GetPatients ,我想您已經有了該模型。 用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM