js不想绑定我的记录列表,已按位置分组

Knockout.js not wanting to bind my list of records that has been grouped by location

本文关键字:位置 列表 不想 绑定 我的 记录 js      更新时间:2023-09-26

我已经试了好几天了,但是运气不好。我正在构建一个ASP。asp.net MVC 5应用程序。我正在创建一个餐厅预订应用程序。其思想是提取一个按位置与实体的linq分组的天数预订,然后将其与signalR一起发送到客户端。在客户端,我想绑定这个分组查询与knockout.js,然后显示它,这就是一切都出错了。将分组预订发送到客户端工作正常,但我似乎无法使映射与淘汰工作。请帮助。

Model on Server Side

var Reservations = db.BistroReservations_Reservations
                                       .GroupBy(reservation => reservation.BistroReservations_Location.Description)
                                       .OrderBy(reservation => reservation.Key.ToString()).ToList();

var context = Microsoft.AspNet.SignalR.GlobalHost.ConnectionManager.GetHubContext<ReservationsHub>();
        context.Clients.All.TestingGroupedReservations(Reservations);

客户端模型

var ReservationsViewModel = function () {
var self = this;
var connection = $.hubConnection();
    var hub = connection.createHubProxy('reservationsHub') 
     var GroupedReservations = ko.mapping.fromJS(reservations);
//Testing -map a collection object to a observalbe and display it underneath the webpage
    hub.on('TestingGroupedReservations', function (reservation) {
        ko.mapping.fromJS(reservation, GroupedReservations);
    });

}

ko.applyBindings(new ReservationsViewModel());  

客户端视图端代码

  <table class="table" data-bind="visible: !loading()">
                <thead class=".h1 glyphicon-bold">Reservations of Selected Day</thead>
                <tbody data-bind="foreach: GroupedReservations">
                    <tr>
                        <td>Shift</td>
                        <td>
                            <table data-bind="foreach:$data">
                                <tbody>
                                    <tr>
                                        <td data-bind="text:BistroReservations_GuestID"></td>
                                    </tr>
                                </tbody>
                            </table>
                        </td>
                    </tr>
                </tbody>
            </table>

Try

this.GroupedReservations = ko.mapping.fromJS(reservations);

这行中"reservations"变量的声明在哪里:

var GroupedReservations = ko.mapping.fromJS(reservations);

从一开始你从来没有设置groupedrereservations属性,你也没有返回self。如果你没有return self,你的所有属性都被认为是私有的。

另外,任何你想要公开访问的东西都需要成为self上的属性。

var ReservationsViewModel = function () {
var self = this;
var connection = $.hubConnection();
    var hub = connection.createHubProxy('reservationsHub') 
         **self.GroupedReservations  = ko.mapping.fromJS(reservations);**
//Testing -map a collection object to a observalbe and display it underneath the webpage
    hub.on('TestingGroupedReservations', function (reservation) {
        ko.mapping.fromJS(reservation, GroupedReservations);
    });
**return self;**
    }