📒 DeskPhoneCreateViewModel Documentation
Namespace:
IrbyImaging.Models
Assembly:IrbyImaging.UI
Author: kvamshi
Created On: 03‑31‑2022
📑 Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- UML Class Diagram
- Class Definition
- Properties
- Data Annotations & Validation
- Usage Examples 📋
- Integration & Workflow 🚀
- Conclusion 🎯
Overview & Purpose
DeskPhoneCreateViewModel is a ViewModel used in the ASP.NET MVC (or Razor Pages) UI layer of the IrbyImaging application. It encapsulates the data and validation rules required to create a new desk phone record in the system.
- 🎯 Goal: Provide a strongly‑typed model for binding form inputs (Phone Number & MAC Address) and enforcing validation rules before persisting to the database.
- 🚀 Intended Use: Used when rendering the "Create Desk Phone" form and processing user submissions.
Dependencies & Technologies
| Technology / Library | Purpose |
|---|---|
System.ComponentModel.DataAnnotations |
Attributes for display and validation. |
| ASP.NET MVC or Razor Pages | Binding & rendering the ViewModel in views. |
Custom CreateViewModel (Base class) |
Shared properties (e.g., Id, audit fields). |
UML Class Diagram
@startuml
class CreateViewModel {
<<abstract>>
+int Id
+DateTime CreatedOn
+string CreatedBy
+DateTime? ModifiedOn
+string ModifiedBy
}
class DeskPhoneCreateViewModel {
+string PhoneNumber
+string MacAddress
}
CreateViewModel <|-- DeskPhoneCreateViewModel
@enduml
Class Definition
using System.ComponentModel.DataAnnotations;
namespace IrbyImaging.Models
{
/// <summary>
/// ViewModel for creating a Desk Phone 🎉
/// Inherits common creation properties from CreateViewModel.
/// </summary>
public class DeskPhoneCreateViewModel : CreateViewModel
{
// Properties defined below...
}
}
Properties
1. PhoneNumber
[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; }
- 📞 What it is: The 10‑digit phone number associated with the desk phone.
- ✅ Validation:
- Must be exactly 10 digits (
0–9). - Displays a custom error message if invalid.
- Must be exactly 10 digits (
2. MacAddress
[Display(Name = "MAC Address")]
public string MacAddress { get; set; }
- 💻 What it is: The hardware MAC address for the desk phone.
- ℹ️ Validation:
- No built‑in regex; validate at controller or client side if needed.
Data Annotations & Validation
| Attribute | Purpose |
|---|---|
[Display(Name = "...")] |
Sets the label text in UI helpers (e.g., @Html.LabelFor). |
[DataType(DataType.PhoneNumber)] |
Hints to render a specialized input (on some templates). |
[RegularExpression(...)] |
Enforces a 10‑digit numeric pattern. Shows a custom error message on mismatch. |
Usage Examples 📋
In an MVC Controller
public class DeskPhoneController : Controller
{
// GET: /DeskPhone/Create
public IActionResult Create()
{
var model = new DeskPhoneCreateViewModel();
return View(model);
}
// POST: /DeskPhone/Create
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(DeskPhoneCreateViewModel model)
{
if (!ModelState.IsValid)
{
// Redisplay form with validation errors
return View(model);
}
// TODO: Map to domain/entity and save to database
// var entity = new DeskPhone { PhoneNumber = model.PhoneNumber, MacAddress = model.MacAddress, ... };
// _dbContext.DeskPhones.Add(entity);
// _dbContext.SaveChanges();
return RedirectToAction("Index");
}
}
In a Razor View
@model IrbyImaging.Models.DeskPhoneCreateViewModel
<form asp-action="Create" method="post">
<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">Create Desk Phone 🚀</button>
</form>
@section Scripts {
@await Html.PartialAsync("_ValidationScriptsPartial")
}
Integration & Workflow 🚀
- User navigates to the Create Desk Phone page.
- The controller returns an empty
DeskPhoneCreateViewModel. - The Razor view renders input fields for Phone Number and MAC Address.
- Client‑side (and server‑side) validation ensures:
- Phone number is 10 digits.
- MAC address is populated (if further validation is applied).
- On submit, the controller:
- Checks
ModelState.IsValid. - Maps ViewModel → Domain/Entity.
- Persists to the database.
- Redirects to an Index or Details page.
- Checks
Conclusion 🎯
The DeskPhoneCreateViewModel is a simple, strongly‑typed representation of the data needed to create desk phones within the IrbyImaging application. By leveraging Data Annotations, it offloads much of the repetitive validation logic and integrates seamlessly with ASP.NET’s model binding and validation pipeline. This makes the UI layer:
- Consistent (labels, error messages).
- Maintainable (attributes live next to properties).
- Secure (prevents bad input from reaching the database).
Feel free to extend this ViewModel with additional properties or custom validation attributes as business requirements evolve! 👍