【发布时间】:2018-08-07 03:22:39
【问题描述】:
我有一个Dictionary<string,List<object>>,我想通过它进行迭代并想在 cshtml 页面上绑定列表值。我得到的值是:
这是我的 cshtml 页面,我正在循环 Dictionary<string,List<object>>
<div class="content">
<div class="quote-slider full-bottom" data-snap-ignore="true">
@foreach (var PriceList in Model.ProductPriceDicList)
{
<div>
<br />
<h4 style="font-weight:bold; color:red">@PriceList.Key</h4>
<table >
@foreach (var DicPriceList in Model.ProductPriceDicList.Values)
{
<tr>
<td style="padding:0px">
<table style="margin-bottom:0px; border:none" >
<tr style="padding:0px">
<td>
@DicPriceList[Convert.ToInt32(PriceList.Key)].SubProductName
</td>
<td>
@DicPriceList[Convert.ToInt32(PriceList.Key)].ProductCost
</td>
<td>
<em class=" fa fa-eye"> </em>
</td>
</tr>
</table>
</td>
</tr>
}
</table>
</div>
}
</div>
我的问题是如何遍历字典,我可以将标题作为键和值作为对象列表。 谢谢
【问题讨论】:
-
@foreach (var DicPriceList in Model.ProductPriceDicList.Values)应该是@foreach (var DicPriceList in Model.ProductPriceDicList)和DicPriceList[Convert.ToInt32(PriceList.Key)]应该是DicPriceList.Key或DicPriceList.Value,具体取决于您是否需要键或值。 -
@mjwills... DicPriceList.Value 给出了完整的对象值,但我想要 DicPriceList.Value.ProductName 之类的东西
-
@mjwills ..thanku 它起作用了..而不是 Value 我使用的是 Values。
标签: c# .net asp.net-mvc dictionary razor