计算不同模型中有多少条目具有特定条件

时间:2013-07-05 14:20:04

标签: c# asp.net-mvc

这是我一直在努力工作很长时间无法解决的问题。

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

我遇到的第一个问题是从一个视图访问两个模型。我已经尝试了大量基于搜索的东西,但似乎无法使任何工作。

第二个问题是如何实际计算。是否可以直接从View中使用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));

1 个答案:

答案 0 :(得分:2)

获取服务器端所需的所有内容并将其传递给视图。您可以先在控制器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>

编辑视图:

{{1}}