简体   繁体   English

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

[英]Nested Collection with KnockoutJs and Asp.net MVC

I have this .net class below 我下面有这个.net类

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

i am trying to map this to a Knockout JS model, my nested collection is not working looks like, what am i doing wrong in the JS model? 我正在尝试将此映射到Knockout JS模型,我的嵌套集合无法正常工作,我在JS模型中做错了什么?

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

Here is my data bind 这是我的数据绑定

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

Here is my ajax to get data 这是我的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]);
                }
            }
        });
    }
}

This is the JSOn that i get from my controller 这是我从控制器获得的JSOn

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

When i disable this line in my view model it works to pull the data, but when i enable this line it does not execute any JS after that point. 当我在视图模型中禁用此行时,它可以拉取数据,但是当我启用此行时,此后它不会执行任何JS。 my assumption is there is something wrong with how i have defined the child collection. 我的假设是我定义子集合的方式有问题。 i do not see any err msg on console on Firefox firebug 我在Firefox firebug的控制台上看不到任何错误消息

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

I don't see any mention of the industries variable in your code, before the initialization of the jobViewModel object. 在初始化jobViewModel对象之前,我的代码中没有提到industries变量。 Could the problem be that you don't have any object set to the industries variable? 问题可能是您没有为industries变量设置任何对象吗?

I think what you should do is that you should remove the line in the jobViewModel initialization which you are referring to. 我认为您应该做的是应该删除所引用的jobViewModel初始化中的行。 Then, since your returned JSON has a property called IndustryName which is an array of string (and no property called industries ), I think you should also rewrite your html binding to something similar to the following (the changes from your original binding is on lines 4 and 7): 然后,由于返回的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>

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

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