π ComputerCreateViewModel.cs Documentation
Welcome to the detailed documentation for ComputerCreateViewModel.cs in the IrbyImaging.UI project. This file defines a ViewModel used in ASP.NET Core MVC to represent the data required when creating a new Computer record.
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- UML Class Diagram
- Key Properties
- Property Reference Table
- Usage Example
- Sample Controller Methods
- Conclusion
1. Overview & Purpose
ComputerCreateViewModel is a ViewModel class that inherits from a base CreateViewModel. It is tailored to the βCreate Computerβ view in an ASP.NET Core MVC application. Its main responsibilities are:
- π― Gather input for properties unique to a Computer (like Asset Number, Unit Number, OS selection).
- π Provide a dropdown list (
SelectListItem) of available Operating Systems. - π Enforce validation rules via DataAnnotations.
This ViewModel travels between the Controller and the View in a typical CRUD (Create, Read, Update, Delete) workflow.
2. Dependencies & Technologies
| Technology / Namespace | Purpose |
|---|---|
Microsoft.AspNetCore.Mvc.Rendering |
Provides SelectListItem for dropdown lists. |
System.Collections.Generic |
Enables use of IEnumerable<T>. |
System.ComponentModel.DataAnnotations |
Supplies [Required] & [Display] attributes. |
Inherits from CreateViewModel |
A base class encapsulating common create properties. |
3. UML Class Diagram
βββββββββββββββββββββββββββββββββ
β CreateViewModel β Base class (not shown)
βββββββββββββββββββββββββββββββββ€
β + Id: int β
β + CreatedDate: DateTime β
β + CreatedBy: string β
ββββββββββββ²βββββββββββββββββββββ
β Inherits
βββββββββββββββββββββββββββββββββ
β ComputerCreateViewModel β
βββββββββββββββββββββββββββββββββ€
β + OperatingSystemId: int β
β + OperatingSystemList: β
β IEnumerable<SelectListItem>β
β + AssetNumber: string β
β + UnitNumber: string β
β + CpsNumber: string β
β + NeedsReview: bool β
βββββββββββββββββββββββββββββββββ
4. Key Properties
Below is a breakdown of each property, its purpose, and how itβs used in the Create view.
-
OperatingSystemId
- Type:
int - DataAnnotation:
[Display(Name = "Operating System")] - Purpose: Holds the selected OSβs primary key. Binds to
<select>in Razor.
- Type:
-
OperatingSystemList
- Type:
IEnumerable<SelectListItem> - Purpose: Populates the dropdown of operating systems in the view.
- Type:
-
AssetNumber
- Type:
string - DataAnnotations:
[Required][Display(Name = "JDE Asset Number")]
- Purpose: Unique identifier from JDE for asset tracking.
- Type:
-
UnitNumber
- Type:
string - DataAnnotations:
[Required][Display(Name = "JDE Unit Number")]
- Purpose: JDE Unit number for inventory.
- Type:
-
CpsNumber
- Type:
string - DataAnnotations:
[Required][Display(Name = "CPS Number")]
- Purpose: Internal tracking number (βCPSβ).
- Type:
-
NeedsReview
- Type:
bool - DataAnnotation:
[Display(Name = "Needs IT Review?")] - Purpose: Flag indicating if the computer should be reviewed by IT.
- Type:
5. Property Reference Table
| Property Name | Type | Required? | Display Label | Description |
|---|---|---|---|---|
| OperatingSystemId | int |
β | Operating System | Selected OS primary key. |
| OperatingSystemList | IEnumerable<SelectListItem> |
β | β | List of OS for dropdown. |
| AssetNumber | string |
β | JDE Asset Number | Asset identifier from JDE. |
| UnitNumber | string |
β | JDE Unit Number | Unit number from JDE systems. |
| CpsNumber | string |
β | CPS Number | CPS tracking number. |
| NeedsReview | bool |
β | Needs IT Review? | Flag for IT review. |
6. Usage Example
6.1. Populating the ViewModel in a Controller (GET)
public IActionResult Create()
{
var vm = new ComputerCreateViewModel
{
OperatingSystemList = _db.OperatingSystems
.Select(os => new SelectListItem {
Value = os.Id.ToString(),
Text = os.Name
})
.ToList()
};
return View(vm);
}
6.2. Handling the Form Submission (POST)
[HttpPost]
public IActionResult Create(ComputerCreateViewModel vm)
{
if (!ModelState.IsValid)
{
// Re-populate dropdown if validation fails
vm.OperatingSystemList = _db.OperatingSystems
.Select(os => new SelectListItem {
Value = os.Id.ToString(),
Text = os.Name
}).ToList();
return View(vm);
}
var computer = new Computer {
AssetNumber = vm.AssetNumber,
UnitNumber = vm.UnitNumber,
CpsNumber = vm.CpsNumber,
OSId = vm.OperatingSystemId,
NeedsReview = vm.NeedsReview
};
_db.Computers.Add(computer);
_db.SaveChanges();
return RedirectToAction("Index");
}
7. Sample Controller Methods
| Action | HTTP Verb | Description |
|---|---|---|
| Create | GET | Renders blank form with OS dropdown. |
| Create | POST | Validates input; on success, persists new Computer entry. |
8. Conclusion
The ComputerCreateViewModel is your strongly-typed contract between the Create view and Controller for adding new Computer records. By leveraging DataAnnotations and a typed OS dropdown, it ensures:
- β Strong Validation
- β Clean Separation between UI and domain
- β Reusability & Testability
Feel free to extend this ViewModel if you need to capture additional metadata (e.g., PurchaseDate, WarrantyInfo) in the future. Happy coding! π
End of Documentation.