π TimeEntryListViewModel.cs Documentation
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Class Diagram (UML)
- TimeEntryListViewModel Breakdown
- 4.1 Class Signature
- 4.2 Properties
- Usage Example
- Sample Code Blocks
- Mermaid UML Diagram
- 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 hasText,Value, andSelectedfor 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!