2
votes

I have a Kendo Grid set up as follows. The url is for the code snippet below, which is an ASP.NET Web Api. When I use "Add new record" on the grid and the press Update, the Post method is called as expected, and the musics parameter exists as expected, however, it is empty. The stringify method has nothing to stringify, in other words. Why is the binding not happening?

dataSource = new kendo.data.DataSource({
  transport: {
...
        create: {
            url: "/api/mywebapi/",
            type: "POST"
        },
        parameterMap: function (data, operation) {
            if (operation !== "read" && data.models) {
                return { models: kendo.stringify(data.models) };
            }
            return { models: kendo.stringify(data) };
        }

...

The Web Api method:

    public HttpResponseMessage Post(IEnumerable<MusicVM> musics)
    {
        ...
    }

I should say that in debug I can see that operation is "create" and that data.models does indeed have the new item I entered in the grid.

2

2 Answers

1
votes

Finally, after years of struggle, I stumbled upon something that works. I changed parameterMap to this:

        parameterMap: function (data, operation) {
            if (operation !== "read" && data.models) {

                var items = {};
                $.each(data.models, function (index, item) {
                    for (var key in item) {
                        items["[" + index + "]" + "." + key] = item[key];
                    }
                });

                return items;
            }

Why this was so difficult, I have no idea, but thanks to Burke Holland for this. http://blogs.telerik.com/kendoui/posts/11-11-07/batch_crud_operations_with_kendo_ui_datasource

0
votes

Instead of using

public HttpResponseMessage Post(IEnumerable<MusicVM> musics)
    {
        ...
    }

Use

public JsonResult Post(string models)
{
//Deserialize string to object
IList<MusicVM> musics= new JavaScriptSerializer().Deserialize<IList<MusicVM>>(models);

return Json(musics)
}

Note that parameterMap: function() send data in serialize format with name models so you should use models as parameter name in your action

you may refer to this post as well