79826583

Date: 2025-11-21 14:15:59
Score: 1
Natty:
Report link

Thank you. I also wonder whether it's better practice to add an extension function, e.g. ToViewModel() for the User class. This way we can separate the logic for conversion between these better compared to having it all on the controller.

This way it make look like so:

public async Task<IActionResult> Index()
{
    var users = await _context.Users
        .Include(u => u.Messages)
        .ToListAsync();

    var vm = users.Select(u => u.ToViewModel());

    return View(vm);
}
Reasons:
  • Blacklisted phrase (0.5): Thank you
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: 92carmnad