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

C# 计算不同模型中有多少条具有特定标准,c#,asp.net-mvc,C#,Asp.net Mvc,这是一个问题,我已经工作了很长时间,但没有结果 基本上我有两个模型:单位和交易,我想要的是当我在单位视图上时,它返回单位的分页列表。我想要的是表中的一个附加列,它返回交易中每个单位有多少个条目具有Trade.Name=model.Name的计数 我遇到的第一个问题是从一个视图访问两个模型。我已经尝试了很多基于搜索的东西,但似乎什么都做不到 第二个问题是如何实际进行计数。可以直接从视图中使用Linq吗?到目前为止,它对我不起作用 提前感谢您的帮助 单元视图的重要部分是: @model PagedL

这是一个问题,我已经工作了很长时间,但没有结果

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

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

第二个问题是如何实际进行计数。可以直接从视图中使用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>

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

编辑视图:

 @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>
@model PagedList.IPagedList
名称
类型
技巧
评级
@foreach(模型中的var项目){
@{ViewBag.ImgUrl=item.Name+“.png”;}
@DisplayFor(modelItem=>item.Unit.Type)
@DisplayFor(modeleItem=>item.Unit.Skill)
@DisplayFor(modelItem=>item.Unit.Rating)
@DisplayFor(modelItem=>item.TradeCount)
}

谢谢您的回复,看起来不错,但我如何将其放在“单位”页面上?我可以查看您的“单位”视图吗?您的意思是我应该创建一个全新的model.cs文件并链接到该文件进行查看?是的。它将帮助您保持组织性并允许更多的扩展性。我将进行编辑,以显示您可以多快地更改视图以使用它。非常感谢您的帮助,我现在将尝试一下,并让您知道它是如何运行的。
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>