计算不同模型中有多少条目具有特定的标准

本文关键字:标准 多少 模型 计算 | 更新日期: 2023-09-27 18:21:06

这是我处理了很长时间的问题,但没有成功。

基本上我有两个模型:单位和交易,我想要的是当我在单位视图上时,它会返回一个分页的单位列表。我想要的是表中的一个额外列,它返回Trades中有多少条目具有Trade.Name=model的计数。每个单元的名称。

我遇到的第一个问题是从一个视图访问两个模型。我在搜索的基础上尝试了很多东西,但似乎都做不到。

第二个问题是如何实际进行计数。是否可以直接从视图中使用Linq?到目前为止,它对我不起作用。

提前感谢或任何帮助!

单位视图的重要部分:

@model PagedList.IPagedList<FTv2.Models.Unit>
<table style="border-width: 1px; border-color:#000000; border-style: solid; border-top: 2px; border-top-color:#000000; border-top-style: solid;">
    <tr>
        <th></th>
        <th>Name</th>
        <th>Type</th>
        <th>Skill</th>
        <th>Rating</th>
    </tr>
@foreach (var item in Model) {
    <tr>
        <td>
            @{ ViewBag.ImgUrl = item.Name + ".png";}
            <a href="/Images/@ViewBag.ImgUrl" data-lightzap="" ><img src="/Images/@ViewBag.ImgUrl" HEIGHT="66" WIDTH="50" ></a>
        </td>
        <td>
            <a href="/ActiveTrades?Name=@item.Name">@Html.DisplayFor(modelItem => item.Name)</a>
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Type)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Skill)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Rating)
        </td>
        <td>
            <!-- this is where I would want the count to go. -->
        </td>
    </tr>
}
</table>

上一期,在视图中未显示任何结果。

以下是控制器的相关部件:

    var units = db.Units;
    var students = db.Units.Select(u => new UnitViewModel()
    {
        Unit = u,
        TradeCount =
               db.Movies.Where(t => t.Name == u.Name).Count()
    });
    return View(students.ToPagedList(pageNumber, pageSize));

计算不同模型中有多少条目具有特定的标准

在服务器端获取所需的一切,并将其传递给视图。您可以先在控制器GET操作中进行计数,然后使用ViewBag传递,或者在视图模型中添加一个属性来保存计数。

    [HttpGet]
    public ActionResult MyView()
    {
       var units = _context.Units.Where(//whatever);
       var viewModels = units.Select(u => new UnitViewModel()
                                           {
                                               Unit=u,
                                               TradeCount =
                                                      context.Trades.Where(t => t.name == u.name).Count()
                                             });
       return View(viewModels);
    }

编辑:

我会为您的视图编写一个视图模型类。因此,视图不再使用List<Unit>的模型,而是使用List<UnitViewModel>

public class UnitViewModel
{
   public Unit Unit {get;set;}
   public int TradeCount {get;set;}
}

编辑视图:

 @model PagedList.IPagedList<FTv2.Models.UnitViewModel>
    <table style="border-width: 1px; border-color:#000000; border-style: solid; border-top: 2px; border-top-color:#000000; border-top-style: solid;">
        <tr>
            <th></th>
            <th>Name</th>
            <th>Type</th>
            <th>Skill</th>
            <th>Rating</th>
        </tr>
    @foreach (var item in Model) {
        <tr>
            <td>
                @{ ViewBag.ImgUrl = item.Name + ".png";}
                <a href="/Images/@ViewBag.ImgUrl" data-lightzap="" ><img src="/Images/@ViewBag.ImgUrl" HEIGHT="66" WIDTH="50" ></a>
            </td>
            <td>
                <a href="/ActiveTrades?Name=@item.Unit.Name">@Html.DisplayFor(modelItem => item.Unit.Name)</a>
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Unit.Type)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Unit.Skill)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Unit.Rating)
            </td>
            <td>
                  @Html.DisplayFor(modelItem => item.TradeCount)
            </td>
        </tr>
    }
    </table>