π‘οΈ SafetyEquipmentCreateViewModel Documentation
Assembly: IrbyImaging.UI
Path:IrbyImaging/Models/SafetyEquipment/SafetyEquipmentCreateViewModel.cs
Author: kvamshi
Created On: 03β31β2022
Last Modified: 03β31β2022
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- Class Definition & UML Diagram
- Properties & Data Annotations
- Usage Scenarios & Examples
- Error Handling & Validation
- Third-Party Integrations
- Conclusion
1. Overview & Purpose
The SafetyEquipmentCreateViewModel class is part of the IrbyImaging web applicationβs UI layer. It serves as a stronglyβtyped model for the βCreate Safety Equipmentβ page in an ASP.NET Core MVC application.
- π₯οΈ Role: Capture user input when registering new safety equipment (e.g., fall protection, hard hats, etc.).
- π― Goal: Provide properties, display metadata, and selectβlist data for rendering a userβfriendly form.
2. Dependencies & Technologies
| Category | Details |
|---|---|
| Framework | ASP.NET Core MVC |
| Language | C# (.NET 6+) |
| Data Annotations | System.ComponentModel.DataAnnotations |
| Select List Rendering | Microsoft.AspNetCore.Mvc.Rendering |
| Base Model | CreateViewModel (common properties) |
3. Key Functional Areas
- Dropdown Population
Holds anIEnumerable<SelectListItem>for operating systems/sizes. - Data Entry Fields
Captures Tested Date, JDE Asset Number, JDE Unit Number, CPS Number. - Display Metadata
Uses[Display(Name="β¦")]and[DataType(DataType.Date)]to render labels & date pickers. - Extensibility
Inherits fromCreateViewModel, reusing common Id, CreatedBy, CreatedDate, etc.
4. Class Definition & UML Diagram
namespace IrbyImaging.Models
{
public class SafetyEquipmentCreateViewModel : CreateViewModel
{
[Display(Name = "Operating System/Size")]
public int OperatingSystemId { get; set; }
public IEnumerable<SelectListItem> OperatingSystemList { get; set; }
[Display(Name = "Tested Date")]
[DataType(DataType.Date)]
public new DateTimeOffset PurchaseDate { get; set; }
[Display(Name = "JDE Asset Number")]
public string AssetNumber { get; set; }
[Display(Name = "JDE Unit Number")]
public string UnitNumber { get; set; }
[Display(Name = "CPS Number")]
public string CpsNumber { get; set; }
}
}
UML Class Diagram
βββββββββββββββββββββββββββββ
β CreateViewModel β
βββββββββββββββββββββββββββββ
β + int Id β
β + string CreatedBy β
β + DateTimeOffset Created β
β + β¦ β
ββββββββββββ¬βββββββββββββββββ
β inherits
ββββββββββββΌβββββββββββββββββ
β SafetyEquipmentCreateViewModel β
βββββββββββββββββββββββββββββ
β + int OperatingSystemId β
β + IEnumerable<SelectListItem> β
β OperatingSystemList β
β + DateTimeOffset PurchaseDate β
β + string AssetNumber β
β + string UnitNumber β
β + string CpsNumber β
βββββββββββββββββββββββββββββ
5. Properties & Data Annotations
| Property | Type | Annotation(s) | Description |
|---|---|---|---|
| OperatingSystemId | int |
[Display(Name="Operating System/Size")] |
Selected OS/size identifier. |
| OperatingSystemList | IEnumerable<SelectListItem> |
β | Populates the OS dropdown. |
| PurchaseDate | DateTimeOffset |
[Display(Name="Tested Date")]<br/>[DataType(Date)] |
Date the equipment was tested/purchased. |
| AssetNumber | string |
[Display(Name="JDE Asset Number")] |
JDE asset tracking number. |
| UnitNumber | string |
[Display(Name="JDE Unit Number")] |
JDE unit identifier. |
| CpsNumber | string |
[Display(Name="CPS Number")] |
Companyβprovided serial number. |
Note: Some
[Required]annotations are commented out for future needs.
6. Usage Scenarios & Examples
6.1 Controller Integration
public class SafetyEquipmentController : Controller
{
private readonly IOperatingSystemService _osService;
public SafetyEquipmentController(IOperatingSystemService osService)
{
_osService = osService;
}
// GET: /SafetyEquipment/Create
public IActionResult Create()
{
var vm = new SafetyEquipmentCreateViewModel
{
OperatingSystemList = _osService.GetAll()
.Select(os => new SelectListItem
{
Value = os.Id.ToString(),
Text = os.Name
})
};
return View(vm);
}
// POST: /SafetyEquipment/Create
[HttpPost]
public IActionResult Create(SafetyEquipmentCreateViewModel model)
{
if (!ModelState.IsValid)
{
// Re-populate dropdown on error
model.OperatingSystemList = _osService.GetAll()
.Select(os => new SelectListItem
{
Value = os.Id.ToString(),
Text = os.Name
});
return View(model);
}
// Map to domain & save
var entity = Mapper.Map<SafetyEquipment>(model);
_equipmentService.Add(entity);
return RedirectToAction("Index");
}
}
6.2 Razor View Sample
@model IrbyImaging.Models.SafetyEquipmentCreateViewModel
<form asp-action="Create" method="post">
<div class="form-group">
<label asp-for="OperatingSystemId"></label>
<select asp-for="OperatingSystemId"
asp-items="Model.OperatingSystemList"
class="form-control">
<option value="">-- Select OS/Size --</option>
</select>
<span asp-validation-for="OperatingSystemId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="PurchaseDate"></label>
<input asp-for="PurchaseDate" class="form-control" type="date" />
<span asp-validation-for="PurchaseDate" class="text-danger"></span>
</div>
<!-- Asset Number -->
<div class="form-group">
<label asp-for="AssetNumber"></label>
<input asp-for="AssetNumber" class="form-control" />
</div>
<!-- Unit Number -->
<div class="form-group">
<label asp-for="UnitNumber"></label>
<input asp-for="UnitNumber" class="form-control" />
</div>
<!-- CPS Number -->
<div class="form-group">
<label asp-for="CpsNumber"></label>
<input asp-for="CpsNumber" class="form-control" />
</div>
<button type="submit" class="btn btn-primary">Create π οΈ</button>
</form>
7. Error Handling & Validation
- Relies on DataAnnotations for client/server validation.
- Invalid model states return the same view with validation messages.
- Developer should re-populate dropdown lists on POST failures.
8. Third-Party Integrations
- AutoMapper (optional) to map between
SafetyEquipmentCreateViewModeland domain entity. - jQuery Validation & Unobtrusive Validation for clientβside form checks.
9. Conclusion
The SafetyEquipmentCreateViewModel provides a clean, metadataβrich surface for collecting all necessary fields to register new safety equipment. By leveraging ASP.NET Coreβs model binding and validation attributes, it ensures:
- β Stronglyβtyped inputs in controllers & views
- β
Automatic label rendering via
[Display] - β
Dropdown generation through
SelectListItem - β
Date formatting via
[DataType(DataType.Date)]
This view model can be extended with additional properties (e.g., Location, Department) and further validation attributes as the requirements evolve.
Happy Coding! π