Alex Petre
Alex Petre

Reputation: 37

kendo grid send json to controller

I'm sending a json from kendo grid when I delete a row but in controller I receive 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"
                });
            });

I put debug here in method and actual parameter 'j' is null, it should be a string (serialize json). In chrome -> debug mode -> network -> I got status 200 ok and in request payload I view the json with all row data, which fact is ok but I'm stuck.

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

Upvotes: 0

Views: 477

Answers (1)

Carsten Franke
Carsten Franke

Reputation: 1669

You are sending a string and pretend that it is JSON (see your contentType and data). It might work if you would change it to something like text/plain.

But you shouldn't even try this! You should create a PatientModel in C# which has the properties FirstName, LastName and so on and expect this very model in your controller method. That way you do not have to serialize and deserialize the data yourself. Considering that you already have an action GetPatients, I'd guess you already have that model. Use it.

Upvotes: 2

Related Questions