使用KnockoutJs和Asp.net MVC嵌套集合

时间:2014-02-01 04:05:05

标签: c# javascript jquery asp.net-mvc knockout.js

我在下面有这个.net类

public class JobDetailsDTO
{
    public string JobName { get; set; }
    public string CompanyName { get; set; }
    public String[] IndustryName;
}

我试图将其映射到Knockout JS模型,我的嵌套集合看起来不像,我在JS模型中做错了什么?

var jobViewModel = {
    jobDetailsDTO: ko.observableArray(),
    currentPage: ko.observable(-1),
    industries: ko.observableArray(industries),
    contentLoadTriggered: false
};

这是我的数据绑定

<div id="jobcontent-wrapper" data-bind="foreach: jobDetailsDTO">
    <label data-bind="text: JobName"></label>
    <span class="jobsize" data-bind="text: CompanyName"> </span>
    <div class="col-md-12" data-bind="foreach: industries">
        <span class="glyphicon glyphicon-heart"></span>
        Industry
        <span class="jobsize" data-bind="text: IndustryName"></span>
    </div>
</div>
<div id="jobcontent-wrapper" data-bind="foreach: jobDetailsDTO">
    <label data-bind="text: JobName"></label>
    <span class="jobsize" data-bind="text: CompanyName"> </span>
    <div class="col-md-12" data-bind="foreach: industries">
        <span class="glyphicon glyphicon-heart"></span>
        Industry
        <span class="jobsize" data-bind="text: IndustryName"></span>
    </div>
</div>

这是我获取数据的ajax

function getData(pageNumber) {
    if (jobViewModel.currentPage() != pageNumber) {
        $.ajax({
        url: "/api/jobservice/getjob",
        type: "get",
        contentType: "application/json",
        data: { id: pageNumber }
        }).done(function (data) {
            if (data.length > 0) {
                for (i = 0; i < data.length; i++) {
                    jobViewModel.jobDetailsDTO.push(data[i]);
                }
            }
        });
    }
}

这是我从我的控制器获得的JSOn

  

[{ “IndustryName”:[ “RIE”, “XSL”, “FWTI”, “QPCAP”, “PPGPUU”], “作业名”: “KLLFBN”, “公司名称”: “CKI”},{” IndustryName “:[” SAF “ ”JIF“, ”MVFG“, ”RPAIP“, ”ALAUKM“], ”作业名“: ”ROULJS“, ”公司名称“: ”LXN“},{ ”IndustryName“:[” IIH ”, “PEM”, “TINE”, “EOAXF”, “ZYJHKK”], “作业名”: “ISUYFV”, “公司名称”: “VZR”}]

当我在视图模型中禁用此行时,它可以提取数据,但是当我启用此行时,它不会在该点之后执行任何JS。我的假设是我如何定义子集合有问题。我在Firefox firebug上的控制台上看不到任何错误消息

var jobViewModel = {
    jobDetailsDTO: ko.observableArray(),
    currentPage: ko.observable(-1),
    industries: ko.observableArray(industries), ///this line
    contentLoadTriggered: false
};

1 个答案:

答案 0 :(得分:1)

industries对象初始化之前,我没有在代码中看到任何jobViewModel变量。问题可能是您没有将任何对象设置为industries变量吗?

我认为你应该做的是你应该删除你所指的jobViewModel初始化中的行。然后,由于您返回的JSON有一个名为IndustryName的属性,它是一个字符串数组(并且没有名为industries的属性),我认为您还应该将您的html绑定重写为类似于以下内容(原始绑定的更改位于第4行和第7行):

1. <div id="jobcontent-wrapper" data-bind="foreach: jobDetailsDTO">
2.     <label data-bind="text: JobName"></label>
3.     <span class="jobsize" data-bind="text: CompanyName"> </span>
4.     <div class="col-md-12" data-bind="foreach: IndustryName">
5.         <span class="glyphicon glyphicon-heart"></span>
6.         Industry
7.         <span class="jobsize" data-bind="text: $data"></span>
8.     </div>
9. </div>