📒 DeskPhoneEditViewModel Documentation

Welcome to the detailed documentation for the DeskPhoneEditViewModel class in the IrbyImaging.UI assembly. This document covers everything from the purpose and high‑level overview down to code snippets, UML diagrams, validation rules, and usage examples. Enjoy! 🎉


📑 Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Class Diagram (UML)
  4. Key Functional Areas
  5. View Model Inheritance & Structure
  6. Property Details & Validation
  7. Usage Examples
  8. Error Handling & Validation Messages
  9. Sample Inputs & Expected Behavior
  10. Conclusion

1. Overview & Purpose

DeskPhoneEditViewModel is a simple View Model within the IrbyImaging.Models namespace, designed to support the Edit Desk Phone feature of the IrbyImaging UI. It:

  • Captures user‑entered values for a desk phone’s Phone Number and MAC Address.
  • Enforces server‑side validation rules via Data Annotations.
  • Provides metadata to assist rendering labels, placeholders, and client‑side validation in an ASP.NET MVC (Razor) application.

Use this class in your MVC Controllers and Razor pages to bind form data, validate on submit, and redisplay errors if any. 📞🔧


2. Dependencies & Technologies

Technology / Library Purpose
.NET Core / .NET Framework Base runtime for MVC Web Applications.
System.ComponentModel.DataAnnotations Provides [Display], [DataType], [RegularExpression].
ASP.NET MVC / Razor Views Model binding & validation in views.
(Optional) jQuery Validation Client-side enactment of DataAnnotations.

3. Class Diagram (UML)

classDiagram
    EditViewModel <|-- DeskPhoneEditViewModel
    class EditViewModel {
        <<abstract>>
        +int Id
        +string Name
        +string Description
        // ... other common edit properties
    }
    class DeskPhoneEditViewModel {
        +string PhoneNumber
        +string MacAddress
    }

Note: EditViewModel is the base for all “Edit” view models in the application. It typically includes common fields like Id, Name, Description, timestamps, etc.


4. Key Functional Areas

  • Form Binding
    Binds user‑submitted form fields (phoneNumber, macAddress) to .NET properties.
  • Server‑Side Validation
    Ensures the phone number consists of exactly 10 digits.
  • Metadata for UI
    Supplies display names for labels and error messages.

5. View Model Inheritance & Structure

namespace IrbyImaging.Models
{
    public class DeskPhoneEditViewModel : EditViewModel
    {
        [Display(Name = "Phone Number")]
        [DataType(DataType.PhoneNumber)]
        [RegularExpression(@"^([0-9]{10})$",
            ErrorMessage = "Invalid Phone Number. Enter the digits only. Ex: 6017094729")]
        public string PhoneNumber { get; set; }

        [Display(Name = "MAC Address")]
        public string MacAddress { get; set; }
    }
}
  • Namespace: IrbyImaging.Models
  • Base Class: EditViewModel
  • Properties:
    • PhoneNumber (string)
    • MacAddress (string)

6. Property Details & Validation

Property Type Display Name Validation
PhoneNumber string Phone Number - Must be exactly 10 digits (^[0-9]{10}$) - DataType: PhoneNumber
MacAddress string MAC Address - No explicit validation; assume formatted as required by downstream logic

🔍 PhoneNumber Regular Expression

^([0-9]{10})$
  • ^ & $ → Anchor the start and end
  • [0-9]{10} → Exactly 10 digits
  • Error Message: “Invalid Phone Number. Enter the digits only. Ex: 6017094729”

7. Usage Examples

ASP.NET MVC / Razor Example

@model IrbyImaging.Models.DeskPhoneEditViewModel

<form asp-action="SaveDeskPhone" method="post" novalidate>
  <div class="form-group">
    @Html.LabelFor(m => m.PhoneNumber)
    @Html.TextBoxFor(m => m.PhoneNumber, new { @class = "form-control" })
    @Html.ValidationMessageFor(m => m.PhoneNumber, "", new { @class = "text-danger" })
  </div>

  <div class="form-group">
    @Html.LabelFor(m => m.MacAddress)
    @Html.TextBoxFor(m => m.MacAddress, new { @class = "form-control" })
    @Html.ValidationMessageFor(m => m.MacAddress, "", new { @class = "text-danger" })
  </div>

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

Controller Action Example

public class DeskPhoneController : Controller
{
    [HttpGet]
    public IActionResult Edit(int id)
    {
        // Fetch existing desk phone data (e.g., from DB or service)
        var existing = _deskPhoneService.GetById(id);
        var model = new DeskPhoneEditViewModel
        {
            Id = existing.Id,
            PhoneNumber = existing.PhoneNumber,
            MacAddress = existing.MacAddress
        };
        return View(model);
    }

    [HttpPost]
    public IActionResult SaveDeskPhone(DeskPhoneEditViewModel model)
    {
        if (!ModelState.IsValid)
        {
            // Validation errors → redisplay form with error messages
            return View("Edit", model);
        }

        // Map back to domain model and persist
        _deskPhoneService.Update(new DeskPhoneDto {
            Id = model.Id,
            PhoneNumber = model.PhoneNumber,
            MacAddress = model.MacAddress
        });

        return RedirectToAction("Index");
    }
}

8. Error Handling & Validation Messages

  • PhoneNumber:
    • On invalid entry (non‑digit characters or wrong length), the message

      Invalid Phone Number. Enter the digits only. Ex: 6017094729
      is shown next to the field.

  • MacAddress:
    • No built‑in validation; errors must be handled in service/business logic if needed.

Features like client‑side validation (via jQuery Unobtrusive) will mirror these messages before the form is even submitted.


9. Sample Inputs & Expected Behavior

Input Value Field Outcome
6017094729 PhoneNumber ✅ Valid (10 digits)
123456789 PhoneNumber ❌ Invalid (only 9 digits)
123-456-7890 PhoneNumber ❌ Invalid (contains dashes)
AA:BB:CC:DD:EE MacAddress ✅ Accepted (no format enforced here)
(empty) PhoneNumber ✅ Allowed unless [Required] is added

Tip: If you need to enforce required fields, add the [Required] attribute above the property.


10. Conclusion

  • DeskPhoneEditViewModel is a lightweight, annotation-driven view model to edit desk phone data in an ASP.NET MVC app.
  • It provides:
    • Metadata for display (labels) and rendering.
    • Validation rules enforcing exactly 10 numeric digits for phone numbers.
  • Integrates seamlessly with Razor form helpers and the MVC model‑binding/validation pipeline.

🎯 Next Steps:

  • Enhance with further annotations (e.g., [Required], [StringLength]) as business rules evolve.
  • Extend client‑side scripts for formatting (e.g., auto‑insert dashes) if desired.
  • Hook up to your service layer or API endpoints to fetch/save data.

Happy coding! 🚀