F# MVC Json Result, on the client empty object after Ajax call -
i have f# mvc app, , trying ajax call , endpoint return list of people, display in table.
type person(credits:int, name:string, serverdatetime:datetime ) = class end type peoplemeta( persons:resizearray<person> ) = member t.size() = 1 [<httpget>] member this.getpeoplemeta()= let person1 = person(1,"a",datetime.now) let person2 = person(2,"b",datetime.now) let persons = new resizearray<person>() persons.add(person1); persons.add(person2); let peoplemeta = peoplemeta( persons ) this.json( peoplemeta , jsonrequestbehavior.allowget )
on html/javascript have this...
<script src="/knockout-2.2.0.js"></script> <script src="knockout.mapping.js"></script> <body> <div id="contentviewmodelajax"> <div> <div data-bind="template: { name: 'personajax-template', foreach: people }"> </div> </div> </div> <script type="text/html" id="personajax-template"> <span data-bind="text: name"></span> <span data-bind="text: credits"></span> <span data-bind="text: serverdatetime"></span> <br /> </script> <script> $(document).ready(function () { execpageviewmodel(); }); var execpageviewmodel = function () { function peopleviewmodel(config) { var self = this; self.config = config; self.people = ko.observablearray([]); self.getpeople = function () { var url = "getpeoplemeta"; var jqxhr = $.ajax({ url: url, type: 'get', cache: false, 'data':self.config }); jqxhr.done(function (data) { console.log(data); console.log(data.persons); self.people(data.persons); ko.applybindings(self, document.getelementbyid(self.elementid)); }); settimeout(self.getpeople, 5000); }; self.exec = function () { self.getpeople(); }; }; var viewmodelconfig = {'elementid':'contentviewmodelajax','personid':1}; var peopleviewmodel = new peopleviewmodel(viewmodelconfig); peopleviewmodel.exec(); }; </script>
the console.log data {} , data.persons undefined. ideas ?
this new code got me result looking
type person() = let mutable _credits = 0 let mutable _name = "" let mutable _serverdatetime = "" member x.credits get() = _credits , set v = _credits <- v member x.name get() = _name , set v = _name <- v member x.serverdatetime get() = _serverdatetime , set v = _serverdatetime <- v type peoplemeta() = let mutable _persons:resizearray<person> = new resizearray<person>() member x.persons get() = _persons , set v = _persons <- v [<httpget>] member this.getpeoplemeta()= let person1 = person() person1.credits <-2 person1.name <- guid.newguid().tostring().substring(1,5) person1.serverdatetime <- datetime.now.tostring() let persons = new resizearray<person>() persons.add(person1); let peoplemeta = peoplemeta( ) peoplemeta.persons <- persons this.json( peoplemeta , jsonrequestbehavior.allowget )
Comments
Post a Comment