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

DataSource.sync() doesn't call create after adding a model to a dataSource generated by ASP.NET MVC ListView Helper

1 Answer 84 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Arlene
Top achievements
Rank 1
Arlene asked on 22 Feb 2013, 06:05 PM
Hi Kendo,


I tried this on 
http://jsfiddle.net/rusev/E3vYH/ and it works fine. Now I have a listView generated by ListView Kendo Helper and I add a model by calling DataSource.add() method, but when I call DataSource.sync() it doesn't call create and I don't see any http activity on my browser's network tab. I copied the code generated by the kendo helper and pasted it into a test html file and it doesn't work neither. It did add the model to the listView but never calls create. Here is my test file:

<!DOCTYPE html>
<html>
    <head>
        <title>Testing Kendoui dataSource</title>
        <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js"></script>
        <script src="http://cdn.kendostatic.com/2012.2.710/js/kendo.all.min.js"></script>
        <script type="text/x-kendo-tmpl" id="template">
            <div data-id = ${CountryCode}> 
 <p>${CountryName}</p>
            </div >
        </script>
        <script>
            $(function () {
                $("#target").kendoListView ({
                    "dataSource": {
                        "transport": {
                            "prefix": "",
                            "read": function () {
                                alert("reading...");
                            },
                            "create": function () {
                                alert("creating...");
                            },
                            "destroy": function () {
                                alert("destroying...");
                            },
                            "update": function () {
                                alert("updating...");
                            }
                        },
                        "type": "aspnetmvc-ajax",
                        "schema": {
                            "data": "Data",
                            "total": "Total",
                            "errors": "Errors",
                            "model": {
                                "id": "SubaccountId",
                                "fields": {
                                    "SubaccountId": {
                                        "type": "number"
                                    },
                                    "CountryCode": {
                                        "type": "string"
                                    },
                                    "CountryName": {
                                        "type": "string"
                                    }
                                }
                            }
                        }
                    },
                    "template": kendo.template($('#template').html()),
                    "selectable": "single"
                });
                $('#addButton').bind("click", function () {
                    var ds = $('#target').data('kendoListView').dataSource;
                    ds.add({

                        SubaccountId: 1,
                        CountryCode: 'US',
                        CountryName: 'United States'

                    });
                    ds.sync();
                });
            });
        </script>
    </head>
    <body>
        <div>
            <button id="addButton">Add Country</button>
        </div>
        <div id="target"></div>
    </body>
</html>

I tried with different jquery and kendo version too. No luck.

How do I make this work.

Thanks

D

1 Answer, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 26 Feb 2013, 11:41 AM
Hello,

A request to the create action will not be made in this case because the field defined as ID is included in the new item. The dataSource uses the ID to determine if the item is new so a request will not be made when specifying a value different then the default one. Remove the "SubaccountId" field from the object should resolve the problem:

$('#addButton').bind("click", function () {
    var ds = $('#target').data('kendoListView').dataSource;
    ds.add({
        CountryCode: 'US',
        CountryName: 'United States'
 
    });
    ds.sync();
});
Regards,
Daniel
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
General Discussions
Asked by
Arlene
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Share this question
or