πŸ“„ 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

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. UML Class Diagram
  4. Key Properties
  5. Property Reference Table
  6. Usage Example
  7. Sample Controller Methods
  8. 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.
  • OperatingSystemList

    • Type: IEnumerable<SelectListItem>
    • Purpose: Populates the dropdown of operating systems in the view.
  • AssetNumber

    • Type: string
    • DataAnnotations:
      • [Required]
      • [Display(Name = "JDE Asset Number")]
    • Purpose: Unique identifier from JDE for asset tracking.
  • UnitNumber

    • Type: string
    • DataAnnotations:
      • [Required]
      • [Display(Name = "JDE Unit Number")]
    • Purpose: JDE Unit number for inventory.
  • CpsNumber

    • Type: string
    • DataAnnotations:
      • [Required]
      • [Display(Name = "CPS Number")]
    • Purpose: Internal tracking number (β€œCPS”).
  • NeedsReview

    • Type: bool
    • DataAnnotation: [Display(Name = "Needs IT Review?")]
    • Purpose: Flag indicating if the computer should be reviewed by IT.

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.