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

2 posts, 0 answers
  1. Arlene
    Arlene avatar
    2 posts
    Member since:
    Nov 2012

    Posted 22 Feb 2013 Link to this post

    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

  2. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 26 Feb 2013 Link to this post

    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!
  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top