在网格上列出虚拟元素属性时,我在淘汰赛的 foreach 绑定方面遇到了一些问题。
尽管 web api 按预期返回 JSON 数据,但敲除未正确显示虚拟元素属性。
我的 UI 显示产品列表,其中一个列是ProductCategory.Name
. 由于某种原因,它只显示每个产品类别的第一次出现。
名称类别 Pr01 Cat01 Pr02 Pr03 Cat02 Pr04 Cat03 Pr05
第 2 和第 5 产品也是“Cat01”类别的产品。但由于某种原因,它不会为他们显示。
我有以下模型:
public class Product
{
[ScaffoldColumn(false)]
public int ProductId { get; set; }
public string Name { get; set; }
public int ProductsCategoryId { get; set; }
public virtual ProductsCategory ProductCategory { get; set; }
}
public class ProductsCategory
{
public int ProductsCategoryId { get; set; }
public string Name { get; set; }
}
这就是我绑定网格的方式:
<tbody data-bind="foreach: products">
<tr>
<td class="left" data-bind="text: $data.Name"></td>
<td class="left" data-bind="text: $data.ProductCategory.Name"></td>
</tr>
</tbody>
这是 JSON:
[{
"$id": "1",
"ProductCategory": {
"$id": "2",
"ProductsCategoryId": 1,
"Reference": "OSOL ",
"Name": "Óculos de Sol",
"ProductsCategoryStatusId": 1
},
"ProductId": 3,
"Reference": "HTHTOD ",
"BarCode": "2122071530085 ",
"Name": "Thin Hard Trivex OD",
"Description": null,
"ProductsBrandId": 1,
"ProductsCategoryId": 1,
"ProductsSupplierId": 1,
"ProductStatusId": 1
}, {
"$id": "3",
"ProductCategory": {
"$ref": "2"
},
"ProductId": 4,
"Reference": "HTHTOE ",
"BarCode": "2122071531163 ",
"Name": "Thin Hard Trivex OE",
"Description": "null",
"ProductsBrandId": 1,
"ProductsCategoryId": 1,
"ProductsSupplierId": 1,
"ProductStatusId": 1
}, {
"$id": "4",
"ProductCategory": {
"$id": "5",
"ProductsCategoryId": 2,
"Reference": "OGRAU ",
"Name": "Óculos de Grau",
"ProductsCategoryStatusId": 1
},
"ProductId": 10,
"Reference": "HTHTOX ",
"BarCode": "2123180206342 ",
"Name": "Thin Hard Trivex OX",
"Description": null,
"ProductsBrandId": 2,
"ProductsCategoryId": 2,
"ProductsSupplierId": 1,
"ProductStatusId": 1
}, {
"$id": "6",
"ProductCategory": {
"$id": "7",
"ProductsCategoryId": 3,
"Reference": "LNTS ",
"Name": "Lentes",
"ProductsCategoryStatusId": 1
},
"ProductId": 16,
"Reference": "HTHTOY ",
"BarCode": "2123192208431 ",
"Name": "Thin Hard Trivex OY",
"Description": null,
"ProductsBrandId": 4,
"ProductsCategoryId": 3,
"ProductsSupplierId": 1,
"ProductStatusId": 1
}, {
"$id": "8",
"ProductCategory": {
"$ref": "2"
},
"ProductId": 12,
"Reference": "HTHTOZ ",
"BarCode": "2123192059538 ",
"Name": "Thin Hard Trivex OZ",
"Description": null,
"ProductsBrandId": 1,
"ProductsCategoryId": 1,
"ProductsSupplierId": 1,
"ProductStatusId": 1
}]
如您所见,ProductsCategory
数据出现一次,然后被同一类别中的下一个产品引用。
关于如何解决此问题以显示网格上所有元素的类别名称的任何建议?