ComputerEditViewModel πŸ–₯️

Namespace: IrbyImaging.Models
Assembly: IrbyImaging.UI
Author: kvamshi
Created: 03‑31‑2022
Last Modified: 03‑31‑2022


πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. Key Functional Areas πŸ› οΈ
  3. Dependencies & Technologies πŸ“¦
  4. Class Diagram πŸ“Š
  5. View Model Properties πŸ“‹
  6. Data Annotations & Validation βœ…
  7. Helper Methods & Utilities 🧩
  8. Error Handling & Return Types πŸ›‘οΈ
  9. API Endpoints Overview 🌐
  10. Third‑party Integrations πŸ”—
  11. Sample Code Blocks πŸ’»
  12. Usage Examples πŸš€
  13. Conclusion 🏁

1. Overview & Purpose

The ComputerEditViewModel is a ViewModel in an ASP.NET Core MVC application, used to encapsulate all the data required by the UI to create or edit a Computer entity. It inherits from a base EditViewModel (which likely provides common fields like Id, CreatedDate, etc.) and extends it with computer‑specific properties:

  • Operating System selection
  • Asset / Unit / CPS numbers
  • Review flags

This separation of concerns (ViewModel vs. domain model) helps to keep UI and business logic loosely coupled, makes validation simpler, and prevents over-posting attacks.


2. Key Functional Areas πŸ› οΈ

  • Operating System Binding
    • Select lists for dropdowns
    • Strongly‑typed OperatingSystemId
  • Asset Tracking
    • AssetNumber, UnitNumber, CpsNumber
  • Review Workflow
    • NeedsReview flag to drive IT review processes
  • Data Validation
    • Using Data Annotations for required fields

3. Dependencies & Technologies πŸ“¦

Dependency Purpose
Microsoft.AspNetCore.Mvc.Rendering SelectListItem for dropdowns in Razor views.
System.ComponentModel.DataAnnotations Validation attributes like [Required].
IrbyImaging.Models Base classes and shared models.

4. Class Diagram πŸ“Š

     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
     β”‚    EditViewModel      β”‚
     β”‚  (Base Class / UI VM) β”‚
     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β–²β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚
               β”‚ inherits
               β”‚
     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
     β”‚ ComputerEditViewModel     β”‚
     │───────────────────────────│
     β”‚ + int    OperatingSystemIdβ”‚
     β”‚ + IEnumerable<SelectListItem> OperatingSystemList β”‚
     β”‚ + string AssetNumber      β”‚
     β”‚ + string UnitNumber       β”‚
     β”‚ + string CpsNumber        β”‚
     β”‚ + bool   NeedsReview      β”‚
     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

5. View Model Properties πŸ“‹

Property Type Attributes Description
OperatingSystemId int [Display(Name="Operating System")] Holds the selected OS ID.
OperatingSystemList IEnumerable<SelectListItem> β€” Provides the dropdown options.
AssetNumber string [Required][Display(Name="JDE Asset Number")] JDE asset tracking number.
UnitNumber string [Required][Display(Name="JDE Unit Number")] JDE unit identifier.
CpsNumber string [Required][Display(Name="CPS Number")] CPS (computer parts) number.
NeedsReview bool [Display(Name="Needs IT Review?")] Flag whether IT review is needed.

6. Data Annotations & Validation βœ…

  • [Required]
    Ensures the user provides a value before the form can be submitted.

  • [Display(Name = "...")]
    Customizes the label rendered in the Razor view (e.g., form labels, validation messages).

ASP.NET Core automatically enforces these validations both client‑side (via unobtrusive validation) and server‑side.


7. Helper Methods & Utilities 🧩

Note:
This class does not define any helper methods or utility functions. Its sole purpose is to serve as a data container for the UI layer.


8. Error Handling & Return Types πŸ›‘οΈ

  • Validation Errors:

    • Collected in ModelState on form submission.
    • Return to the view with validation summaries/highlights.
  • No custom exceptions are thrown directly by this ViewModel.


9. API Endpoints Overview 🌐

This ViewModel is not directly tied to any Web API endpoint.
Instead, it is used in MVC Controller actions:

// GET: Computer/Edit/5
public async Task<IActionResult> Edit(int id)
{
    var model = await _computerService.GetForEditAsync(id);
    return View(model);
}

// POST: Computer/Edit/5
[HttpPost]
public async Task<IActionResult> Edit(ComputerEditViewModel model)
{
    if (!ModelState.IsValid)
        return View(model);

    await _computerService.UpdateAsync(model);
    return RedirectToAction("Index");
}

10. Third‑party Integrations πŸ”—

  • None – only built‑in ASP.NET Core MVC components are used.

11. Sample Code Blocks πŸ’»

11.1. Setting up the OS List in the Controller

public async Task<IActionResult> Create()
{
    var model = new ComputerEditViewModel
    {
        OperatingSystemList = await _lookupService.GetOsSelectListAsync()
    };
    return View(model);
}

11.2. Razor Form Snippet

@model IrbyImaging.Models.ComputerEditViewModel

<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"></select>
    <span asp-validation-for="OperatingSystemId" class="text-danger"></span>
  </div>

  <div class="form-group">
    <label asp-for="AssetNumber"></label>
    <input asp-for="AssetNumber" class="form-control" />
    <span asp-validation-for="AssetNumber" class="text-danger"></span>
  </div>

  <!-- Similar blocks for UnitNumber, CpsNumber, NeedsReview -->

  <button type="submit" class="btn btn-primary">Save</button>
</form>

12. Usage Examples πŸš€

// In your ComputerController.cs
public class ComputerController : Controller
{
    private readonly IComputerService _computerService;
    private readonly ILookupService _lookupService;

    public ComputerController(IComputerService computerService, ILookupService lookupService)
    {
        _computerService   = computerService;
        _lookupService     = lookupService;
    }

    // PREPARE EDIT PAGE
    public async Task<IActionResult> Edit(int id)
    {
        var vm = await _computerService.GetForEditAsync(id);
        vm.OperatingSystemList = await _lookupService.GetOsSelectListAsync();
        return View(vm);
    }

    // HANDLE POST
    [HttpPost]
    public async Task<IActionResult> Edit(ComputerEditViewModel vm)
    {
        if (!ModelState.IsValid)
        {
            vm.OperatingSystemList = await _lookupService.GetOsSelectListAsync();
            return View(vm);
        }

        await _computerService.UpdateAsync(vm);
        return RedirectToAction(nameof(Index));
    }
}

13. Conclusion 🏁

The ComputerEditViewModel is a lean, focused definition of all the fields required to edit a computer record in the UI:

  • 🎯 Clearly separates UI concerns from domain logic
  • 🎨 Leverages ASP.NET Core’s data‑annotation validation
  • πŸ“Š Simplifies the populating of dropdown lists
  • πŸ”’ Reduces risk of over‑posting by explicitly listing only the fields you want to receive

By following this pattern, you’ll maintain clean, testable, and secure UI logic throughout your ASP.NET Core MVC application.