kgst
kgst

Reputation: 243

Counting how many entries in a different model have a certain criteria

This is a problem I have been working on for a very long time to no avail.

Basically I have two Models: Units and Trades, what i want is when I am on the View for Units, which returns a paged list of the units. What I want is an additional column to the table which returns a count of how many entries in Trades have Trade.Name = model.Name for each Unit.

The first problem I am having is accessing two models from one view. I have tried tons of things based on searching, but can't seem to be able to make anything work.

The second problem is how to actually do the count. Is it possible to use Linq directly from the View? It hasn't been working for me so far.

Thanks in advance or any help!

The important part of the Units view:

@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="/[email protected]">@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>

Last issue, not showing any results in the View.

Here's the relevant parts of the controller:

    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));

Upvotes: 0

Views: 126

Answers (1)

Ryan Bennett
Ryan Bennett

Reputation: 3432

Get everything you need server side and pass it to the view. You can do the count in the controller GET action first, and pass it using ViewBag or add a property in your view model to hold the counts.

    [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);

    }

EDIT:

I would write a view model class for your view. So instead of the view using a model of List<Unit>, now it uses List<UnitViewModel>.

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

EDIT VIEW:

 @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="/[email protected]">@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>

Upvotes: 2

Related Questions