I have an asp.net MVC core application. I am trying to query database in one of my controller's action. How can I add this dto into my View? Or is there any other to retrieve data by grouping and display on the view.
Here is my action:
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);
}
}
}
Member
526 Points
2726 Posts
Group By Question
Aug 26, 2019 11:12 AM|cenk1536|LINK
Hi,
I have an asp.net MVC core application. I am trying to query database in one of my controller's action. How can I add this dto into my View? Or is there any other to retrieve data by grouping and display on the view.
Here is my action:
Here is my View:
Member
11 Points
19 Posts
Re: Group By Question
Aug 26, 2019 12:56 PM|Koss86|LINK
other ways to upload data to DTO Class?
you can use custom mapping in SqlConnection. Still have to use grouping.
Member
11 Points
19 Posts
Re: Group By Question
Aug 26, 2019 01:03 PM|Koss86|LINK
See! I use sql.
( reader.MapToModel<SchedulerDTO>(); - my custom mapper from IDataReader to DTOClass)
Member
526 Points
2726 Posts
Re: Group By Question
Aug 26, 2019 01:16 PM|cenk1536|LINK
What I am trying to say is, I am getting this error below. Since I can not add more than one model in the View, I need some other way to solve this.
Cannot implicitly convert type 'Microsoft.AspNetCore.Mvc.ViewResult' to 'System.Collections.Generic.List<GameMonitor.Models.GameBanksDto>'
Member
11 Points
19 Posts
Re: Group By Question
Aug 26, 2019 01:28 PM|Koss86|LINK
https://stackoverflow.com/questions/51816947/cannot-implicitly-convert-type-microsoft-aspnetcore-mvc-badrequestobjectresult