This is a migrated thread and some comments may be shown as answers.

CRUD with MVC Controller

2 Answers 505 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Simon
Top achievements
Rank 1
Simon asked on 16 Apr 2012, 01:08 PM
Hello,

I'm trying to implement the grid CRUD operation using a MVC controller.  As you can see the grid vistualization is enabled and dealing with an OData web service.

<script>
    $(document).ready(function () {
        $("#grid").kendoGrid({
            dataSource: {
                schema: {
                    model: {
                        id: "RecordID",
                        fields: {
                            RecordID: { editable: false, nullable: true },
                            FirstName: { editable: true },
                            LastName: { editable: true }
                        }
                    }
                },
                type: "odata",
                serverPaging: true,
                serverSorting: true,
                pageSize: 100,
                batch: false,
                transport: {
                    read: "http://localhost:1625/Data/GetPatients",
                    create: { url: "http://localhost:1625/Data/Create", contentType: "application/json; charset=utf-8", type: "POST" },
                    update: { url: "http://localhost:1625/Data/Update", contentType: "application/json; charset=utf-8", type: "POST" },
                    destroy: { url: "http://localhost:1625/Data/Destroy", contentType: "application/json; charset=utf-8", type: "POST", dataType: "json" },
                    parameterMap: function (data, operation) {
                        if (operation !== "read") {
                            return { jsonData: kendo.stringify(data) };
                        } else {
                            return kendo.data.transports["odata"].parameterMap(data, operation);
                        }
                    }
 
                }
 
            },
            height: 500,
            scrollable: {
                virtual: true
            },
            editable: true,
            sortable: true,
            toolbar: ["create", "save"],
            columns: ["RecordID", "FirstName", "LastName", { command: "destroy"}]
        });
    });
</script>

Here's the code of the destroy method in the MVC controller:
<System.Web.Mvc.HttpPost()> _
Public Function Destroy(ByVal jsonData As List(Of Patient)) As System.Web.Mvc.ActionResult
 
    'Code to delete the record...
 
    Return Json(Nothing)
 
End Function

I think I'm close to the solution.  The Destroy method is called as expected but the jsonData parameter is always empty.  I tried several permutation ( jsonData parameter as string / Patient / List(Of Patient) / IEnumerable(Of Patient) ) without success.

I saw some examples on the web but none of them dealing with OData.

What am I missing here?

Best regards,

Simon







2 Answers, 1 is accepted

Sort by
0
Alexander Valchev
Telerik team
answered on 19 Apr 2012, 09:45 AM
Hi Simon,

MVC actions has nothing to do with the odata protocol.
Your Grid configuration looks OK, but in order to achieve your goal it is necessary to implement your own logic to map the dataSource parameters to the correct format and handle the paging and sorting of the data on the server.

In addition, I would recommend to check the sample projects from our code library and GitHub page as they show KendoUI integration with various server side technologies.

I hope this information will help.

Regards,
Alexander Valchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Simon
Top achievements
Rank 1
answered on 19 Apr 2012, 03:46 PM
Hello Alexander,

The problem I was about getting the jsonData as parameter with the destroy method.

I did solve my problem by extracting the JSON request string from the System.Web.HttpContext.Current.Request.InputStream.

Dim jsonString as String
Dim reader As New IO.StreamReader(httpContext.Request.InputStream)
Dim javaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer
Dim myPatient as Patient
 
System.Web.HttpContext.Current.Request.InputStream.Position = 0
jsonString = reader.ReadToEnd()
 
myPatient = javaScriptSerializer.Deserialize(Of Patient)(jsonString)

Here's my final implementation of the parameterMap
parameterMap: function (data, operation) {
    if (operation !== "read") {
        return kendo.stringify(data);
    } else {
        return kendo.data.transports["odata"].parameterMap(data, operation);
    }
}

Simon

P.S.  This post should be marked as answer... but I can't flag it because this is my own question.  Kendo team, can you fix this?
Tags
Grid
Asked by
Simon
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
Simon
Top achievements
Rank 1
Share this question
or