Question Group By Question

raysefo

Well-known member
Joined
Feb 22, 2019
Messages
362
Programming Experience
10+
Hi,

I have an asp.net MVC core application. I am trying to query a database in one of my controller's action. How can I add this dto into my View? Or is there any other way to retrieve data by grouping and display on the view. I am getting this error: Cannot implicitly convert type 'Microsoft.AspNetCore.Mvc.ViewResult' to 'System.Collections.Generic.List<GameMonitor.Models.GameBanksDto>' Since I can not add more than one model in the view, I need to figure out something else.

Here is my action:
C#:
namespace GameMonitor.Controllers
{
    public class GameBanksController : Controller
    {
        private readonly GameContext _context;

        public GameBanksController(GameContext context)
        {
            _context = context;
        }

        // GET: GameBanks
        public async Task<List<GameBanksDto>> Index()
        {
            //Group By games
            var games = await _context.GameBanks.GroupBy(g =>new {g.ProductCode,g.UnitPrice,g.ProductDescription})
                .Select(gcs => new GameBanksDto()
                {
                    ProductCode = gcs.Key.ProductCode,
                    ProductDescription = gcs.Key.ProductDescription,
                    UnitPrice = gcs.Key.UnitPrice,
                    Quantity = gcs.Sum(g => g.Quantity)


                }).ToListAsync();

            return View(games);
        }
 

     
    }
}

Here is my View:
C#:
@model IEnumerable<GameMonitor.Models.GameBanks>


@{
    ViewData["Title"] = "Index";
}

<h2>Games</h2>

@*<p>
        <a asp-action="Create">Create New</a>
    </p>*@
<table class="table">
    <thead>
        <tr>

            <th>
                @Html.DisplayNameFor(model => model.ProductCode)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Quantity)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.RequestDateTime)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Used)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ProductDescription)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.UnitPrice)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Status)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>

                <td>
                    @Html.DisplayFor(modelItem => item.ProductCode)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Quantity)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.RequestDateTime)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Used)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ProductDescription)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.UnitPrice)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Status)
                </td>
                <td>
                    <a asp-action="Details" asp-route-id="@item.GameBankId">Details</a>

                </td>
            </tr>
        }
    </tbody>
</table>
 
Last edited:
Back
Top Bottom