π¨οΈ OfficePrinterEditViewModel Documentation
Assembly:
IrbyImaging.UI
File:IrbyImaging/Models/OfficePrinter/OfficePrinterEditViewModel.cs
Author: kvamshi
Created: 03-31-2022
Last Modified: 03-31-2022
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Class Diagram (UML)
- Key Functional Areas
- 4.1 Inheritance
- 4.2 Properties
- 4.3 Data Annotations
- Usage Scenarios & Examples
- Related Classes & View Models
- Error Handling & Validation
- Conclusion
π Overview & Purpose
The OfficePrinterEditViewModel is a ViewModel used within the IrbyImaging UI layer to drive edit screens for office printers. It encapsulates only the fields required when creating or updating an office printer record, and enriches them with metadata for display and validation.
Main Goals
- Provide a strongly-typed model for edit forms
- Leverage data annotations for client/server validation
- Decouple UI concerns from domain or database entities
π§ Dependencies & Technologies
| Technology / Namespace | Purpose |
|---|---|
| System | Base .NET types (e.g., DateTimeOffset) |
| System.ComponentModel.DataAnnotations | Validation and display metadata |
| IrbyImaging.Models | Base EditViewModel and other shared contracts |
This ViewModel is suitable for ASP.NET MVC or ASP.NET Core MVC projects.
π Class Diagram (UML)
classDiagram
class EditViewModel {
<<abstract>>
+ Guid Id
+ string CreatedBy
+ DateTimeOffset CreatedOn
+ string ModifiedBy
+ DateTimeOffset? ModifiedOn
}
class OfficePrinterEditViewModel {
+ string Company
+ DateTimeOffset ContractEndDate
}
EditViewModel <|-- OfficePrinterEditViewModel
Note:
EditViewModelis assumed to define common edit properties likeId, audit fields, etc.
π Key Functional Areas
4.1 Inheritance
- Base Class:
EditViewModel - Implication: Inherits standard properties (e.g.,
Id, audit fields) and possibly validation logic used across all edit screens.
4.2 Properties
| Property | Type | Required? | Description |
|---|---|---|---|
| Company | string |
β | Name of the printer vendor or leasing partner. |
| ContractEndDate | DateTimeOffset |
β | When the maintenance/lease contract expires. |
4.3 Data Annotations
[Display(Name = "Contract End Date")]
Β Β β’ Human-friendly label in forms and validation messages.[DataType(DataType.Date)]
Β Β β’ Renders a<input type="date" />(HTML5 date picker) in Razor EditorFor helpers.
π Usage Scenarios & Examples
Below are typical patterns for leveraging OfficePrinterEditViewModel in an ASP.NET MVC application.
5.1 Controller Example
using IrbyImaging.Models;
using Microsoft.AspNetCore.Mvc;
using System;
public class OfficePrinterController : Controller
{
// GET: /OfficePrinter/Edit/{id}
public IActionResult Edit(Guid id)
{
// 1. Retrieve domain entity from database
var entity = _printerService.GetById(id);
if (entity == null) return NotFound();
// 2. Map to ViewModel
var vm = new OfficePrinterEditViewModel
{
Id = entity.Id,
Company = entity.Company,
ContractEndDate = entity.ContractEndDate
};
return View(vm);
}
// POST: /OfficePrinter/Edit
[HttpPost]
public IActionResult Edit(OfficePrinterEditViewModel vm)
{
if (!ModelState.IsValid)
return View(vm);
// 3. Map back to domain model
var updateEntity = new OfficePrinter
{
Id = vm.Id,
Company = vm.Company,
ContractEndDate = vm.ContractEndDate
};
_printerService.Update(updateEntity);
return RedirectToAction("Index");
}
}
5.2 Razor View Example
@model IrbyImaging.Models.OfficePrinterEditViewModel
<h2>Edit Office Printer π¨οΈ</h2>
<form asp-action="Edit" method="post">
@Html.HiddenFor(m => m.Id)
<div class="form-group">
@Html.LabelFor(m => m.Company)
@Html.TextBoxFor(m => m.Company, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Company)
</div>
<div class="form-group">
@Html.LabelFor(m => m.ContractEndDate)
@Html.EditorFor(m => m.ContractEndDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(m => m.ContractEndDate)
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
5.3 Mapping to Domain Model
Using AutoMapper (optional):
CreateMap<OfficePrinter, OfficePrinterEditViewModel>().ReverseMap();
π Related Classes & View Models
- EditViewModel (Base)
Defines common edit properties:Id,CreatedOn,ModifiedOn, etc. - OfficePrinterViewModel (Read-Only)
Might contain additional computed fields used for listing printers.
β Error Handling & Validation
- Server-side validation is driven by Data Annotations:
- Missing
CompanyorContractEndDatewill triggerModelStateerrors.
- Missing
- Client-side validation (unobtrusive) automatically reads the same annotations to prevent round-trips.
π Conclusion
OfficePrinterEditViewModel is a lightweight, annotation-driven class designed to streamline the edit experience for office printer records in the IrbyImaging UI. By inheriting from a shared EditViewModel, it enforces consistency across forms, leverages builtβin ASP.NET validation, and cleanly separates UI concerns from domain logic.
Use this ViewModel to:
- Render edit forms with proper labels
- Validate user input at both client/server layers
- Map easily to and from your domain or data-layer entities
Happy coding! β¨