📋 CrewViewModel.cs Documentation
Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- View Models Breakdown
- Data Annotations & Validation
- Dropdowns & SelectList Integration
- UML Class Diagram
- Sample Usage in Controller
- Sample Usage in Razor View
- Best Practices & Tips
- Conclusion 🚀
1. Overview & Purpose
CrewViewModel.cs contains a set of ViewModel classes that bridge data between your ASP.NET Core MVC controllers and Razor views for managing crews, their employees, equipment, cost codes, and jobs.
This file lives under IrbyImaging/Models and is primarily used to:
- Capture user input via forms
- Populate dropdowns/multi-select lists
- Apply validation rules on client/server side
2. Dependencies & Technologies
| Category | Libraries / Types |
|---|---|
| Core Framework | .NET 6+ / .NET Core |
| MVC Support | Microsoft.AspNetCore.Mvc.Rendering (SelectListItem) |
| Data Annotation / Validation | System.ComponentModel.DataAnnotations |
| Domain Layer | IrbyImaging.DomainClasses |
| Collections & Types | System, System.Collections.Generic |
3. Key Functional Areas
- Form Data Binding: Capturing crew details, selected employees and equipment.
- Validation: Ensuring required fields (e.g.,
CrewName,JobId) are supplied. - Select Lists: Populating dropdowns for Jobs, Employees, Equipment, Cost Codes, Classes, Per Diem Types.
- Nested ViewModels: Breaking down complex forms into sub-models (employee rows, equipment rows, etc.).
4. View Models Breakdown
CrewViewModel
public class CrewViewModel
{
public Guid CrewId { get; set; }
[Required]
public string CrewName { get; set; }
[Required]
public Guid JobId { get; set; }
// Multi-select: employees & equipment
public List<CrewEmployeeViewModel> CrewEmployees { get; set; }
= new List<CrewEmployeeViewModel>();
public List<CrewEquipmentViewModel> CrewEquipment { get; set; }
= new List<CrewEquipmentViewModel>();
// Dropdown lists
public List<CrewJobViewModel> Jobs { get; set; }
public IEnumerable<SelectListItem> Employees { get; set; }
public List<SelectListItem> Equipment { get; set; }
public List<CrewCostCodeViewModel> CostCodes { get; set; }
public List<CrewClassViewModel> Classes { get; set; }
public List<SelectListItem> PerDiemTypes { get; set; }
}
- CrewId: Unique identifier (GUID).
- CrewName & JobId: Marked
[Required]to enforce validation. - CrewEmployees: List of employees assigned to the crew.
- CrewEquipment: List of equipment items assigned.
- Jobs, Employees, Equipment, CostCodes, Classes, PerDiemTypes: Populate HTML
<select>elements.
CrewEmployeeViewModel
public class CrewEmployeeViewModel
{
public Guid EmployeeId { get; set; }
public string CostCode { get; set; }
public string Class { get; set; }
public Guid? CostCodeId { get; set; }
public Guid? JobClassificationId { get; set; }
public string StartTime { get; set; }
public string PerDiemType { get; set; }
public decimal? PerDiemRate { get; set; }
}
- EmployeeId: Reference to the selected employee.
- CostCode, Class, CostCodeId, JobClassificationId: Additional grouping/classification fields.
- StartTime, PerDiemType, PerDiemRate: Per‑diem setup per employee.
CrewEquipmentViewModel
public class CrewEquipmentViewModel
{
public Guid EquipmentId { get; set; }
public Guid JobId { get; set; }
public Guid? CostCodeId { get; set; }
public decimal? WorkedHours { get; set; }
}
- EquipmentId, JobId, CostCodeId: Equipment‑to‑job mapping.
- WorkedHours: Time tracking for equipment.
CrewCostCodeViewModel
| Property | Type | Description |
|---|---|---|
| Text | string | Display text in dropdown |
| Value | string | Underlying value (often CostCodeId) |
| JobId | string | Associated job for filtering |
| CostCodeType | CostCodeType | Enum: Labor, Material, Misc, etc. |
| CostCodeNumber | string | Formatted cost code number |
Note:
CostCodeTypecomes fromIrbyImaging.DomainClasses.
CrewJobViewModel
public class CrewJobViewModel
{
public string Text { get; set; }
public string Value { get; set; }
public string JobNumber { get; set; }
}
- Used to populate the Job dropdown list.
CrewClassViewModel
public class CrewClassViewModel
{
public string Text { get; set; }
public string Value { get; set; }
public string JobId { get; set; }
public int CostCodeNumber { get; set; }
}
- Represents a classification list filtered by job.
5. Data Annotations & Validation
[Required]on CrewName and JobId ensures server- and client-side validation.- You can enhance validation by adding attributes like
[StringLength],[Range], or custom validators.
6. Dropdowns & SelectList Integration
- IEnumerable and List are used to bind dropdowns.
- Populate these in your controller:
var vm = new CrewViewModel {
Jobs = jobs.Select(j => new CrewJobViewModel {
Text = j.Name,
Value = j.Id.ToString(),
JobNumber = j.Number
}).ToList(),
Employees = allEmployees.Select(e => new SelectListItem {
Text = $"{e.FirstName} {e.LastName}",
Value = e.Id.ToString()
})
};
7. UML Class Diagram 🛠️
classDiagram
CrewViewModel --> "0..*" CrewEmployeeViewModel : CrewEmployees
CrewViewModel --> "0..*" CrewEquipmentViewModel : CrewEquipment
CrewViewModel --> "0..*" CrewCostCodeViewModel : CostCodes
CrewViewModel --> "0..*" CrewJobViewModel : Jobs
CrewViewModel --> "0..*" CrewClassViewModel : Classes
class CrewViewModel {
+Guid CrewId
+string CrewName
+Guid JobId
+List~CrewEmployeeViewModel~ CrewEmployees
+List~CrewEquipmentViewModel~ CrewEquipment
+List~CrewJobViewModel~ Jobs
+IEnumerable~SelectListItem~ Employees
+List~SelectListItem~ Equipment
+List~CrewCostCodeViewModel~ CostCodes
+List~CrewClassViewModel~ Classes
+List~SelectListItem~ PerDiemTypes
}
Copy/paste into a Mermaid-enabled editor to render.
8. Sample Usage in Controller
public class CrewController : Controller
{
private readonly IJobService _jobService;
private readonly IEmployeeService _employeeService;
public CrewController(IJobService jobService, IEmployeeService employeeService)
{
_jobService = jobService;
_employeeService = employeeService;
}
public IActionResult Create()
{
var jobs = _jobService.GetAllJobs();
var employees = _employeeService.GetAllEmployees();
var vm = new CrewViewModel
{
Jobs = jobs.Select(j => new CrewJobViewModel {
Text = j.Name, Value = j.Id.ToString(), JobNumber = j.Number
}).ToList(),
Employees = employees.Select(e => new SelectListItem {
Text = $"{e.LastName}, {e.FirstName}",
Value = e.Id.ToString()
})
};
return View(vm);
}
[HttpPost]
public IActionResult Create(CrewViewModel model)
{
if (!ModelState.IsValid)
{
// repopulate dropdowns...
return View(model);
}
// Map to domain object + save...
return RedirectToAction("Index");
}
}
9. Sample Usage in Razor View
@model CrewViewModel
<form asp-action="Create" method="post">
<div class="form-group">
<label asp-for="CrewName"></label>
<input asp-for="CrewName" class="form-control"/>
<span asp-validation-for="CrewName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="JobId">Select Job</label>
<select asp-for="JobId" asp-items="@(new SelectList(Model.Jobs, "Value", "Text"))"
class="form-control"></select>
</div>
<!-- Multi-select employees -->
<div class="form-group">
<label>Employees</label>
<select asp-for="CrewEmployees" asp-items="Model.Employees"
class="form-control" multiple></select>
</div>
<button type="submit" class="btn btn-primary">Save Crew</button>
</form>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
10. Best Practices & Tips 💡
- Repopulate dropdowns after a failed
POSTto avoid empty lists on redisplay. - Use AutoMapper or manual mapping to translate between ViewModels and Domain/DTO classes.
- Extend validation with custom attributes for cross-field checks (e.g., ensure at least one employee selected).
- Consider partial views for repeating sections (e.g., employee row, equipment row).
11. Conclusion 🚀
The CrewViewModel.cs file is a central piece in the ASP.NET Core MVC application for managing crews. By clearly separating domain logic (in IrbyImaging.DomainClasses) from UI concerns (in these ViewModels), you ensure:
- Clean separation of concerns
- Strongly-typed form binding
- Simple validation wiring
Use these models as the backbone of your views, controllers, and JavaScript-driven UI components to deliver a consistent and maintainable user experience.