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

Kendo Web API Datasource no data returned

6 Answers 252 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Roy
Top achievements
Rank 1
Roy asked on 03 Feb 2014, 08:27 AM
We are trying the trial version to determine if we are going to purchase it but my testing has run into a problem. I am trying to load a data source in JavaScript so that will be called when a id from the treeview is passed on selection ( that part is working) , but when I call my java script function the data source creates but no data is returned, I have included my JavaScript function here and I have also included the method from the controller it is to call, can someone tell me why I get no data back, i have created an alert at the end of the function to show the data but the length is always zero for the loop does not execute

here is the JavaScript

    var updateAgencySystemInfo = function (agencyId, agencySystemModel) {
        var ds = new kendo.data.DataSource({
            trasnport: {
                read: {
                    type:"POST",
                    url: "/Administrator/GetAgencySystemInfo",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json"
                },
                parameterMap: function (options, operation) {
                    switch (operation) {
                        case "read":
                            return JSON.stringify(options);
                            break;
                        //case "destroy":
                        //    return JSON.stringify(options);
                        //    break;
                        //case "create":
                        //case "update":
                        //    return JSON.stringify(options);
                        //    break;
                    }
                }
            },
            schema: {
                model: {
                    id: "AgencySystemID", // the identifier of the model
                    fields: {
                        "AgencySystemID": { type: "number", editable: false },
                        "AgencyID": { type: "number", editable: false },
                        "Description": { type: "string" },
                        "DatabaseTypeID": { type: "number" },
                        "DataSource": { type: "string" },
                        "InitalCatalog": { type: "string" },
                        "UserID": { type: "string" },
                        "Password": { type: "string" },
                        "LastImportStartDateTime": { type: "date" },
                        "LastImportEndDateTime": { type: "date" },
                        "NextImportDateTime": { type: "date" },
                        "UpToDateTime": { type: "date" },
                        "ImportFrequency": { type: "number" },
                        "ImportFrequencyUnitID": { type: "number" },
                        "MaxImportWindow": { type: "number" },
                        "IsActive": { type: "boolean" }
                    }
                }
            }
        });
        ds.read({ Id: agencyId });
        
        ds.fetch();
        var datasourcedata = ds.data();
        
        for (var i = 0; i < datasourcedata.length; i++) {
            var dataitem = datasourcedata[i].Description;
            alert(dataitem);
        }



        return datasourcedata.length;
    };


Now here is the method from the controller the datasource is to call for the data.

        [HttpPost]
        public JsonResult GetAgencySystemInfo(long? agencyID)
        {
            var agencySystemInfo = (from p in dc.mst_AgencySystem where p.AgencyID == agencyID select p).ToList();

            return Json(agencySystemInfo, JsonRequestBehavior.AllowGet);

        }


Thanks,
rglunt68

6 Answers, 1 is accepted

Sort by
0
Roy
Top achievements
Rank 1
answered on 03 Feb 2014, 08:52 AM
Ok I did notice I spelled transport incorrectly,funny though I did not get any javascript errors , once i did get that correct my controller method did get called, but i was still getting zero records for loop over after calling the datasource read and fetch, i think
I need to hookup a change event on the datasource because it is async , i will follow up after I have tried this.
0
Roy
Top achievements
Rank 1
answered on 03 Feb 2014, 10:12 AM
ok I now got the change event to fire on the datasource, but now i need to loop over each row on a specific field , are there any examples on how to do this.
0
Daniel
Telerik team
answered on 05 Feb 2014, 08:46 AM
Hello Roy,

You can loop through the data in the change event as in any other case - by iterating over the items in the data array which can be found using the dataSource data method
var ds = new kendo.data.DataSource({
    change: function() {
        var data = this.data();
        for (var i = 0; i < data.length; i++) {
            var description = data[i].Description;           
        }
    }
    ...
Besides using the change event you could also specify a callback function for the fetch method which will be called after the data is loaded from the server.

Regards,
Daniel
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Roy
Top achievements
Rank 1
answered on 06 Feb 2014, 07:32 PM
Daniel,

 I was doing that in my change event, but my data.length was 0. So maybe I should explain what I am trying to do and maybe you can tell me if what I am doing is wrong or write, or even better would be a workable example.

I am using MVC 5 and I am using a controller that returns the model to build a tree view on the left (which is working fine). What I want to happen is when I click on a tree node that has no children is to then use the database id value from tree to build another kendo data source to bind to edit controls on the right say a datepicker and an textedit control, so that when changes are done to those edit controls it will update the data base based on the id of the selected tree node. The tree is a different database table then the edit controls on the right, this is why I needed two different data sources. And eventually there will be a grid on the right to even a third database table. But for now I am just trying to get the tree on the left to one datasource to drive the second datasource on the left for the edit fields.

Ok with that all said, I do have the tree loading and I do have the second data source firing on the select event of the tree and it is
accepting a param being passed that is the id from the custom attribute on the tree node. What it seems to be is my second  datasource does not seem to return correctly, sometimes and then other times not. I have attached some JavaScript file and my cshtml file.

 

,Roy

0
Accepted
Daniel
Telerik team
answered on 10 Feb 2014, 03:27 PM
Hello again Roy,

The problem could be caused by not using the correct key for the data. You should use "agencyId" instead of "agency-id" in order to retrieve the value with the data method when the attribute is named "data-agency-id" e.g.
if (!dataItem.hasChildren) {
    agencyId = node.data("agencyId");
Besides that, the code looks OK. If the data is still not available in the change event after correcting the key, please check if a request is made and what is the response from the server.

Regards,
Daniel
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Roy
Top achievements
Rank 1
answered on 10 Feb 2014, 06:28 PM
Yes you are right, That was my mistake and  I did not even see it. So now I can add an create and update events to that datasource so that when I can add new records or update records. Again thanks fro catching my typo.

Tags
General Discussions
Asked by
Roy
Top achievements
Rank 1
Answers by
Roy
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or