📄 CreateFieldTimesheetViewModel.cs Documentation


🔖 Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. UML Class Diagram
  5. View Models & Data Models
    5.1. CreateFieldTimesheetViewModel
    5.2. JobsCreateModel
    5.3. CostCodeCreateModel
    5.4. ClassCreateModel
  6. Sample Usage in Controller & View
    6.1. Populating the ViewModel (Controller)
    6.2. Rendering Drop‑Downs (Razor View)
  7. Third‑Party Integrations & Helpers
  8. Error Handling & Return Types
  9. Conclusion

🧐 Overview & Purpose

The CreateFieldTimesheetViewModel and its nested models are UI‑focused data containers used to render and process a “Create Field Timesheet” form in an ASP.NET Core MVC application.
They encapsulate all the drop‑down lists and selection data needed when field employees log their hours by:

  • Selecting an Employee, Job, Cost Code, Class
  • Assigning Work Orders, Pay Codes, Equipment, and Union Codes

🛠 Dependencies & Technologies

Dependency Purpose
Microsoft.AspNetCore.Mvc.Rendering Provides SelectListItem for building HTML <select> lists
IrbyImaging.DomainClasses Defines domain entities such as CostCodeType enumeration
DocumentFormat.OpenXml.Office2010.ExcelAc (Imported but not used in this file – possibly leftover)

Note: No direct API endpoints or helper methods exist in this file; it strictly defines data models.


🚀 Key Functional Areas

  • Drop‑down population: Contains lists for all user choices (Employees, Jobs, etc.).
  • Separation of concerns: Distinct small classes (JobsCreateModel, CostCodeCreateModel, ClassCreateModel) for specialized selection data.
  • Clean UI binding: Enables strongly‑typed Razor views using asp-for and asp-items.

📊 UML Class Diagram

classDiagram
    class CreateFieldTimesheetViewModel {
        + List<SelectListItem> Employees
        + List<JobsCreateModel> Jobs
        + List<CostCodeCreateModel> CostCodes
        + List<ClassCreateModel> Classes
        + List<SelectListItem> WorkOrders
        + List<SelectListItem> PayCodes
        + List<SelectListItem> Equipment
        + List<SelectListItem> UnionCodes
    }
    class JobsCreateModel {
        + string Text
        + string Value
        + string JobId
    }
    class CostCodeCreateModel {
        + string Text
        + string Value
        + CostCodeType CostCodeType
        + string CostCodeNumber
        + string JobId
    }
    class ClassCreateModel {
        + string Text
        + string Value
        + int CostCodeNumber
        + string CostCodeName
        + string JobId
    }

    CreateFieldTimesheetViewModel --> JobsCreateModel
    CreateFieldTimesheetViewModel --> CostCodeCreateModel
    CreateFieldTimesheetViewModel --> ClassCreateModel

💾 View Models & Data Models

5.1 CreateFieldTimesheetViewModel

Property Type Description
Employees List Drop‑down list of employees (Name/ID pairs)
Jobs List List of available jobs with metadata
CostCodes List Cost codes tied to selected jobs
Classes List Cost‑code classes (sub‑categories)
WorkOrders List Available work orders
PayCodes List Pay code options (regular, overtime, etc.)
Equipment List Equipment pick list
UnionCodes List Union classification codes

Usage:

  • Passed from Controller to Razor View for binding
  • Enables dynamic rendering of <select> controls

5.2 JobsCreateModel

Property Type Description
Text string Display text for the job
Value string Underlying value (e.g., ID)
JobId string Unique identifier for a job

Scenario: Render a drop‑down of “Jobs” in the form.


5.3 CostCodeCreateModel

Property Type Description
Text string Display text (e.g., “Electrical”)
Value string Underlying cost code ID
CostCodeType CostCodeType Enum: differentiates code categories
CostCodeNumber string Code identifier (e.g., “CC-1001”)
JobId string Associated job for filtering

Scenario: Filter cost codes based on selected job and category.


5.4 ClassCreateModel

Property Type Description
Text string Display name for the class
Value string Unique class ID
CostCodeNumber int Numeric wrapper around cost code
CostCodeName string Human‑readable cost code name
JobId string Associated job for context filtering

Scenario: Further categorize cost codes by “Class” (e.g., Labor, Materials).


🧩 Sample Usage in Controller & View

6.1 Populating the ViewModel (Controller)

public class FieldTimesheetController : Controller
{
    private readonly IEmployeeService _employeeSvc;
    private readonly IJobService _jobSvc;
    private readonly ICostCodeService _costCodeSvc;

    public FieldTimesheetController(
        IEmployeeService employeeSvc,
        IJobService jobSvc,
        ICostCodeService costCodeSvc)
    {
        _employeeSvc = employeeSvc;
        _jobSvc      = jobSvc;
        _costCodeSvc = costCodeSvc;
    }

    public IActionResult Create()
    {
        var vm = new CreateFieldTimesheetViewModel
        {
            Employees = _employeeSvc
                .GetAll()
                .Select(e => new SelectListItem(e.FullName, e.Id.ToString()))
                .ToList(),

            Jobs = _jobSvc
                .GetActiveJobs()
                .Select(j => new JobsCreateModel {
                    Text  = j.Name,
                    Value = j.Id.ToString(),
                    JobId = j.Id.ToString()
                }).ToList(),

            CostCodes = _costCodeSvc
                .GetAll()
                .Select(cc => new CostCodeCreateModel {
                    Text            = cc.Name,
                    Value           = cc.Id.ToString(),
                    CostCodeType    = cc.Type,
                    CostCodeNumber  = cc.Number,
                    JobId           = cc.JobId.ToString()
                }).ToList(),

            // Similar initialization for Classes, WorkOrders, PayCodes, Equipment, UnionCodes...
        };

        return View(vm);
    }
}

6.2 Rendering Drop‑Downs (Razor View)

@model IrbyImaging.UI.Models.CreateFieldTimesheetViewModel

<form asp-action="Create" method="post">
  <div class="form-group">
    <label asp-for="Employees">Employee</label>
    <select asp-for="Employees" asp-items="Model.Employees"
            class="form-control"></select>
  </div>

  <div class="form-group">
    <label>Job</label>
    <select name="SelectedJob" 
            class="form-control"
            onchange="onJobChange(this.value)">
      @foreach(var job in Model.Jobs) {
        <option value="@job.Value">@job.Text</option>
      }
    </select>
  </div>

  <!-- CostCodes drop-down dynamically filtered by selected Job -->
  <div class="form-group">
    <label>Cost Code</label>
    <select id="costCodeSelect"
            class="form-control">
      @foreach(var cc in Model.CostCodes.Where(cc => cc.JobId == Model.Jobs.First().JobId)) {
        <option value="@cc.Value">@cc.Text (@cc.CostCodeNumber)</option>
      }
    </select>
  </div>
  
  <button type="submit" class="btn btn-primary">Submit</button>
</form>

<script>
  function onJobChange(jobId) {
    // TODO: Ajax call to filter CostCodes and Classes by jobId
  }
</script>

🔗 Third‑Party Integrations & Helpers

  • Microsoft.AspNetCore.Mvc.Rendering
    Provides SelectListItem for all drop‑down sources.
  • IrbyImaging.DomainClasses
    Supplies CostCodeType enumeration and possibly other domain definitions.

There are no custom helper methods in this file; it's purely data only.


⚠️ Error Handling & Return Types

  • No explicit error handling is present in this file.
  • The view models assume that controller code has validated and loaded non-null lists.
  • If any list is null, the Razor view may throw a NullReferenceException when iterating.

Recommendation:

Employees = _employeeSvc?.GetAll()?.Select(...).ToList() ?? new List<SelectListItem>();

✅ Conclusion

The CreateFieldTimesheetViewModel.cs file is a crucial UI layer component that aggregates all necessary selection data for building a field timesheet form. By leveraging strongly‑typed lists and small helper models (JobsCreateModel, CostCodeCreateModel, ClassCreateModel), it ensures:

  • Strong binding in Razor views
  • Clear separation between UI concerns and domain logic
  • Flexibility in filtering dependent lists (e.g., Cost Codes by Job)

💡 Extend this model with additional properties (e.g., SelectedEmployeeId, SelectedJobId) to bind user inputs directly back to your controller on form POSTs.

Happy coding! 🚀