πŸ“„ TimeEntryListViewModel.cs Documentation


πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Class Diagram (UML)
  4. TimeEntryListViewModel Breakdown
  5. Usage Example
  6. Sample Code Blocks
  7. Mermaid UML Diagram
  8. Conclusion

🎯 Overview & Purpose

The TimeEntryListViewModel is a simple ViewModel used in an ASP.NET Core MVC application to transport a collection of time entries (timesheets) alongside a list of selectable employees to a Razor view.

  • Intended Use: Populate a view (e.g., Timesheet listing page) where the front‐end needs both the timesheet data and a drop‑down of employees.
  • Key Benefit: Encapsulates related data for a specific view, adhering to the Single Responsibility Principle and promoting a clean separation between domain models and presentation layer.

βš™οΈ Dependencies & Technologies

Technology / Package Purpose
IrbyImaging.DomainClasses Contains the FieldTimesheet domain entity.
Microsoft.AspNetCore.Mvc.Rendering Provides the SelectListItem type for HTML <select>.
System.Collections.Generic .NET Core generic collection types (List<T>).
  • Framework: ASP.NET Core MVC
  • Language: C#

🧩 Class Diagram (UML)

classDiagram
    class TimeEntryListViewModel {
        +List~FieldTimesheet~ Timesheets
        +List~SelectListItem~ Employees
    }

TimeEntryListViewModel Breakdown

4.1 Class Signature

namespace IrbyImaging.Models
{
    public class TimeEntryListViewModel
    {
        // ...
    }
}
  • Namespace: IrbyImaging.Models
  • Accessibility: public – accessible across the application.

4.2 Properties

Property Type Description
Timesheets List<FieldTimesheet> A collection of timesheet entries to display in the view.
Employees List<SelectListItem> A list of <option> items for an employee drop-down selector.

FieldTimesheet is a domain class representing a single timesheet record (e.g., date, hours, project).
SelectListItem is an MVC helper type, each instance has Text, Value, and Selected for HTML <select> construction.


Usage Example

Below is a simplified controller action showing how to populate and use TimeEntryListViewModel:

using IrbyImaging.DomainClasses;
using IrbyImaging.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Linq;

public class TimesheetController : Controller
{
    private readonly ApplicationDbContext _context;

    public TimesheetController(ApplicationDbContext context)
    {
        _context = context;
    }

    public IActionResult Index()
    {
        // Fetch all timesheets from the database
        var timesheets = _context.FieldTimesheets.ToList();

        // Prepare employee select list
        var employees = _context.Employees
            .Select(e => new SelectListItem 
            { 
                Text = $"{e.FirstName} {e.LastName}", 
                Value = e.Id.ToString() 
            })
            .ToList();

        // Build the ViewModel
        var viewModel = new TimeEntryListViewModel
        {
            Timesheets = timesheets,
            Employees = employees
        };

        return View(viewModel);
    }
}

πŸ“¦ Sample Code Blocks

1. The Original ViewModel File

using IrbyImaging.DomainClasses;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Collections.Generic;

namespace IrbyImaging.Models
{
    public class TimeEntryListViewModel
    {
        public List<FieldTimesheet> Timesheets { get; set; }
        public List<SelectListItem> Employees { get; set; }
    }
}

2. Razor View Snippet (Index.cshtml)

@model IrbyImaging.Models.TimeEntryListViewModel

<h2>Timesheet Entries</h2>

<form method="post" asp-action="FilterByEmployee">
    <select asp-for="Employees" asp-items="Model.Employees">
        <option value="">-- Select Employee --</option>
    </select>
    <button type="submit">Filter</button>
</form>

<table>
    <thead>
        <tr>
            <th>Date</th>
            <th>Project</th>
            <th>Hours</th>
        </tr>
    </thead>
    <tbody>
    @foreach (var entry in Model.Timesheets)
    {
        <tr>
            <td>@entry.Date.ToShortDateString()</td>
            <td>@entry.ProjectName</td>
            <td>@entry.HoursWorked</td>
        </tr>
    }
    </tbody>
</table>

Mermaid UML Diagram

classDiagram
    class FieldTimesheet {
        +int Id
        +DateTime Date
        +string ProjectName
        +decimal HoursWorked
        +int EmployeeId
    }

    class SelectListItem {
        +string Text
        +string Value
        +bool Selected
    }

    class TimeEntryListViewModel {
        +List~FieldTimesheet~ Timesheets
        +List~SelectListItem~ Employees
    }

    FieldTimesheet <|-- TimeEntryListViewModel : contains >
    SelectListItem <|-- TimeEntryListViewModel : contains >

πŸ”š Conclusion

The TimeEntryListViewModel is a lightweight container designed to shuttle both timesheet records and employee selection data from the controller to the view in an ASP.NET Core MVC application. By grouping related data into a single ViewModel:

  • We reduce coupling between view logic and domain entities.
  • We clarify intent in controller actions and Razor pages.
  • We simplify unit testing of both controllers and views.

πŸš€ Whether you’re rendering complex forms or simple listings, following this pattern keeps your codebase clean, maintainable, and scalable!