Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebAPI Model Binding from JSON

I am creating an application using Durandal, with WebAPI as the server. I have a KendoUI grid that displays the data from the server correctly and functions properly until the POST or PUT methods are called. Here is my GET method: enter image description here

and you can see that that data binds to the UI (used the data-bind extensibility in Durandal to change to kendo bindings): enter image description here

Then I edit the data in the Grid and it passes the changes inside the request to the server as you can see in this Fiddler result: enter image description here

On the server side I cannot get the data that is passed from the client to bind to anything I place as a parameter for the method on the POST or PUT. enter image description here

I realize this is a couple different technologies to troubleshoot (e.g. Durandal, KnockoutJs, Kendo DataBinding, and WebAPI) but I think the fundamentals are working, the data is retrieved and bound to the UI and it is posted back when changed, but the WebAPI endpoint cannot bind to the data.

How can I get the passed "models" array to bind through the ModelBinding structure in WebAPI?

UPDATE- Here is the helpful JSFiddle that gave me the correct Content-Type to add: http://jsfiddle.net/Xhrrj/1/

new kendo.data.DataSource({ transport: { read: { type: "POST", url: "../cccs/service.svc/SupplierSearch", contentType: "application/json; charset=utf-8", dataType: 'json'...

this is coming from the Telerik forum here

like image 218
John Maloney Avatar asked May 04 '13 15:05

John Maloney


People also ask

What is model binding in Web API?

The model binding system: Retrieves data from various sources such as route data, form fields, and query strings. Provides the data to controllers and Razor pages in method parameters and public properties. Converts string data to . NET types.

How do we do parameter binding in Web API?

When Web API calls a method on a controller, it must set values for the parameters, a process called binding. By default, Web API uses the following rules to bind parameters: If the parameter is a "simple" type, Web API tries to get the value from the URI.

How do I use FromUri in Web API?

So, if you want to override the above default behaviour and force Web API to read a complex type from the URI, add the [FromUri] attribute to the parameter. To force Web API to read a simple type from the request body, add the [FromBody] attribute to the parameter.


1 Answers

It looks as if it was mixing up form-urlencoded with json format - if you lookat the decoded string it is sending models= and then urlencoded JSON objects follow.

like image 117
Joanna Derks Avatar answered Oct 20 '22 02:10

Joanna Derks