简体   繁体   English

将数据推送到ko.observableArray

[英]Pushing data into ko.observableArray

I'm having problems getting this to work. 我在使它工作时遇到问题。 I have a form in a partial view in which I would like to populate a select field from a ko.observableArray. 我在局部视图中有一个表单,我想在其中填充ko.observableArray中的选择字段。 In my javascript i can see my incoming data being returned via signalR but for some reason it's not being pushed to my observableArray. 在我的JavaScript中,我可以看到我的传入数据是通过signalR返回的,但是由于某种原因它没有被推送到我的observableArray。 Here is my javascript: 这是我的JavaScript:

var AddAthleteToRosterVm = function(user) {
var self = this;

// reference the auto-generated proxy for the hub
var teamMangHub = $.connection.teamMangHub;

// Athlete and Team arrays for select list
self.Athletes = ko.observableArray();
self.Teams = new Array();

// assign athlete data
teamMangHub.client.getAthletes = function(data) {
    // populate Athletes array
    self.Athletes.push(data); // data is not being pushed here.  Athletes array remains empty.
};

$.connection.hub.start().done(function() {

    // retrieve the athletes from the server
    teamMangHub.server.retrieveAthletes(user);

});
};

Here's my partial view: 这是我的部分观点:

<h3 class="text-center">Add Athlete To Roster</h3>

@using (Html.BeginForm("AddAthleteToRoster", "CoachRosterManagement", FormMethod.Post,
new {@class = "text-center", id="athleteToRosterForm"}))
{
   @Html.AntiForgeryToken()

  <fieldset class="myFormSpace">
    <legend>Athlete Info</legend>

    <p>
        @Html.LabelFor(x => x.InputAthleteToRoster.AthleteId)<br />
        <select name="InputAthleteToRoster.AthleteId" data-bind="options: Athletes, optionsText: 'FirstName', value: 'Id', optionsCaption: 'Select'"></select>
    </p>
    <p>
        @Html.LabelFor(x => x.InputAthleteToRoster.CoachesTeamId)<br />
        @Html.DropDownListFor(x => x.InputAthleteToRoster.CoachesTeamId,
        new SelectList(Enumerable.Empty<SelectListItem>()), "Select")
    </p>
    <button type="submit">Add Athlete</button>
</fieldset>
}

and this is the script that applies the binding from the view that calls partial view: 这是从调用局部视图的视图中应用绑定的脚本:

@section scripts
{

<script src="~/signalr/hubs"></script>
<script src="~/Scripts/MyScripts/AddAthleteToRoster.js"></script>
<script>
    var user = "@User.Identity.Name";


    ko.applyBindings(new AddAthleteToRosterVm(user));
</script>

}

On a side note, when I change the Athlete array from a ko.observableArray to a standard array such as: 附带说明一下,当我将Athlete数组从ko.observableArray更改为标准数组时,例如:

 self.Athletes = new Array();

then the data will get pushed, but it still will not render in the select field on my partial view. 那么数据将被推送,但仍不会在我的局部视图的选择字段中呈现。

Your observableArray is probably not empty after pushing data ; 推送data后,您的observableArray可能不为空; in order to inspect it, you need to inspect the result of self.Athletes() , since the observableArray is a function (so self.Athletes itself will show as an empty array even if there are elements in it). 为了检查它,您需要检查self.Athletes()的结果,因为observableArray是一个函数 (所以self.Athletes本身将显示为空数组,即使其中包含元素)。

Assuming data is an array of objects, each representing an athlete, it's probably not binding to your select list properly because you're pushing all of your data into one array element with 假设data是一个对象数组,每个对象代表一个运动员,则它可能未正确绑定到选择列表,因为您将所有数据推入一个数组元素中,

self.Athletes.push(data);

Since data is an array itself, you'll want to append the elements of data like this instead: 由于data本身就是数组,因此您需要添加如下data元素:

self.Athletes.push.apply(self.Athletes, data);

(or maybe simply assign it with self.Athletes(data) , depending on your requirements). (或根据您的要求,简单地将其分配给self.Athletes(data) )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM