0

如何在cshtml中显示键和计数数据

这是类:

      public class ViewModelData
       {
         public int Key { get; set; }
         public int Count { get; set; }
       }

这是控制器:

    public ActionResult Trigger()
       {
        var result = repository.Paths.GroupBy(x => x.SmokeCar)
                                    .Select(x => new ViewModelData
                                    {
                                        Key = x.Key,
                                        Count = x.Count()
                                    }).ToList();

        return View(result);
         }

这是cshtml

      @model IEnumerable CessationPath.Domain.Entities.ViewModelData
          <div>
        <span>Count:</span>
          </div>
4

2 回答 2

0

循环遍历传递的集合并打印它。

@model IEnumerable<CessationPath.Domain.Entities.ViewModelData>
@foreach(var item in Model)
{
    <div>
        @item.Key.ToString()
        <span> Count: @item.Count.ToString()</span>
    </div>
}
于 2013-04-01T20:51:00.633 回答
0

要显示记录计数:

@model IEnumerable<CessationPath.Domain.Entities.ViewModelData>
<div>
    <span>Count:</span> @Model.Count()
</div>

然后显示每条记录的内容:

<ul>
@foreach (var item in Model) {
    <li>@item.Key, @item.Count</li>
}
</ul>
于 2013-04-01T20:51:50.823 回答