knockout.js - Why is KnockoutJS not showing the same number of objects as my Web API controller? -
the solution same in question: how observablearray's length?
...but question unique in same situation have easier time finding answer did.
i'm new knockout , web api. i've built following based upon similar code found in few different tutorials.
function myviewmodel() { var self = this; self.objects = ko.observablearray([]); $.getjson("http://localhost:xxxx/api/getobjects", function (data,status,xhr) { self.objects(data); alert(xhr + " " + status + ", " + "objects returned: " + self.objects.length); } } ko.applybindings(new myviewmodel());
in controller, goal filter objects table in database matchingid
matches current user's id. have in controller:
public iqueryable<myobject> getobjects() { this.applicationdbcontext - new applicationdbcontext(); this.usermanager = new usermanager<applicationuser>(this.applicationdbcontext)); var user = usermanager.findbyid(user.identity.getuserid()); var query = db.myobjects.where(a => a.matchingid == user.id); return query; }
i've stepped through , confirmed query pulling desired records database, yet response received on client side empty. here output of alert:
alert(xhr + " " + status + ", " + "objects returned: " + self.objects.length); // yields this: // "200 success, objects returned: 0"
there 4 valid objects in query on server-side. can me figure out why didn't transfer through?
ko.observablearray have length property, property of observablearray function itself, not of array:
https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/function/length
to length of enclosed array, invoke observablearray call length: self.object().length
. example in code, change alert to:
alert(xhr + " " + status + ", " + "objects returned: " + self.objects().length);
and should give '4' you're after.
Comments
Post a Comment