📄 OfficePrinterCreateViewModel.cs Documentation

Path: IrbyImaging/Models/OfficePrinter/OfficePrinterCreateViewModel.cs
Assembly: IrbyImaging.UI
Author: kvamshi
Created: 03‑31‑2022
Last Modified: 03‑31‑2022


📑 Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. View Models & Data Models
  5. Data Validation & Attributes
  6. UML Class Diagram
  7. Sample Code Snippet
  8. Usage Example
  9. Conclusion

🔍 Overview & Purpose

OfficePrinterCreateViewModel is a simple ViewModel used in the UI layer of the IrbyImaging application to capture data when creating a new Office Printer record.

  • It inherits from a base CreateViewModel (common fields such as Name, Description, etc. are presumed defined there).
  • Introduces printer‑specific fields:
    • Company: The vendor or manufacturer of the printer.
    • ContractEndDate: When the service contract expires (renders as a date picker in a Razor view).

This ViewModel enables strong‑typed binding in ASP.NET MVC/Razor pages and enforces validation through Data Annotations.


🛠 Dependencies & Technologies

Technology / Namespace Purpose
System Core .NET types (e.g., DateTimeOffset).
System.ComponentModel.DataAnnotations Provides validation and UI‑hints attributes.
IrbyImaging.Models Contains CreateViewModel and other models.
ASP.NET MVC / Razor Renders the ViewModel in Create/Edit views.

🚀 Key Functional Areas

  1. Inheritance
    • Extends CreateViewModel to reuse common creation‑time properties (e.g., Name, Description).
  2. Data Capture
    • Captures Company name and ContractEndDate specific to office printers.
  3. UI–Hinting & Validation
    • Uses Data Annotations to guide rendering and client/server‑side validation.

🔧 View Models & Data Models

Class Signature

public class OfficePrinterCreateViewModel : CreateViewModel
{
    public string Company { get; set; }

    [Display(Name = "Contract End Date")]
    [DataType(DataType.Date)]
    public DateTimeOffset ContractEndDate { get; set; }
}

Property Reference

Property Type Attributes Description
Company string — Printer vendor/manufacturer name.
ContractEndDate DateTimeOffset [Display(Name="Contract End Date")][DataType(Date)] Service contract expiration date.

✔️ Data Validation & Attributes

  • [Display(Name = "...")]
    • Changes the label shown in Razor views or validation summaries.
  • [DataType(DataType.Date)]
    • Renders a date picker (HTML5 <input type="date">) in modern browsers.
  • Server‑side validation will automatically enforce type‑safety and required format (e.g., valid date).

📊 UML Class Diagram

classDiagram
    CreateViewModel <|-- OfficePrinterCreateViewModel
    class CreateViewModel {
      <<abstract>>
      + string Name
      + string Description
      + ... // other common create fields
    }
    class OfficePrinterCreateViewModel {
      + string Company
      + DateTimeOffset ContractEndDate
    }

💻 Sample Code Snippet

// OfficePrinterCreateViewModel.cs
using System;
using System.ComponentModel.DataAnnotations;

namespace IrbyImaging.Models
{
    public class OfficePrinterCreateViewModel : CreateViewModel
    {
        public string Company { get; set; }

        [Display(Name = "Contract End Date")]
        [DataType(DataType.Date)]
        public DateTimeOffset ContractEndDate { get; set; }
    }
}

📋 Usage Example

1. Controller Action (ASP.NET MVC)

public class OfficePrintersController : Controller
{
    // Display the Create form
    public IActionResult Create()
    {
        return View(new OfficePrinterCreateViewModel());
    }

    // Handle form submission
    [HttpPost]
    public IActionResult Create(OfficePrinterCreateViewModel model)
    {
        if (!ModelState.IsValid)
        {
            // Validation failed; redisplay form
            return View(model);
        }

        // Map to domain entity, save to DB, etc.
        var printerEntity = new OfficePrinter
        {
            Name = model.Name,
            Description = model.Description,
            Company = model.Company,
            ContractEndDate = model.ContractEndDate
        };
        _printerService.Add(printerEntity);

        return RedirectToAction("Index");
    }
}

2. Razor View (Create.cshtml)

@model IrbyImaging.Models.OfficePrinterCreateViewModel

<form asp-action="Create" method="post">
  <div class="form-group">
    <label asp-for="Name"></label>
    <input asp-for="Name" class="form-control" />
    <span asp-validation-for="Name" class="text-danger"></span>
  </div>

  <div class="form-group">
    <label asp-for="Description"></label>
    <textarea asp-for="Description" class="form-control"></textarea>
  </div>

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

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

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

🏁 Conclusion

The OfficePrinterCreateViewModel is a lightweight, strongly‑typed model that streamlines the process of creating Office Printer records in the UI. By leveraging Data Annotations, it:

  • Provides clear labels and validation for user input.
  • Ensures a consistent contract between your web forms and your backend logic.
  • Promotes reusability via inheritance from CreateViewModel.

Use this ViewModel in your MVC controllers and Razor views to deliver a robust, type‑safe user experience when onboarding new printers into the IrbyImaging system.

Happy coding! 🚀