0

我有一个 F# mvc 应用程序,我试图让 ajax 调用和端点返回一个人员列表,然后显示在一个表格中。

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 ) 

在 Html/JavaScript 我有这个...

    <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>

console.log 数据为 {},data.persons 未定义。有任何想法吗 ?

4

1 回答 1

0

这个新代码让我得到了我正在寻找的结果

type Person() = 
    let mutable _credits = 0
    let mutable _name  = ""
    let mutable _serverDateTime = ""
    member x.credits with get() = _credits and set v = _credits <- v
    member x.name with get() = _name and set v = _name <- v
    member x.serverDateTime with get() = _serverDateTime and set v = _serverDateTime <- v

type PeopleMeta() = 
    let mutable _persons:ResizeArray<Person>  = new ResizeArray<Person>() 
    member x.persons with get() = _persons and 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 ) 
于 2013-05-19T21:58:49.950 回答