π DeskPhoneDetailViewModel.cs Documentation
Assembly :
IrbyImaging.UI
Namespace :IrbyImaging.Models
Author : kvamshi
Created : 03β31β2022
Last Modified : 03β31β2022
ποΈ Table of Contents
- Overview & Purpose
- Key Functional Areas π
- Dependencies & Technologies π οΈ
- DeskPhoneDetailViewModel Class Details
- Usage Scenarios & Examples π‘
- Integration in Controllers & Views π
- [Conclusion & Best Practices π―]
1. Overview & Purpose
DeskPhoneDetailViewModel is a simple ViewModel designed to encapsulate the details of a desk phone in the IrbyImaging UI.
- β Purpose: To transfer just the right data (phone number & MAC address, plus any inherited detail fields) from the controller to a view or API response.
- π Use Case: Display, edit or validate desk phone information in ASP.NET MVC or Razor Pages.
2. Key Functional Areas π
| Area | Description |
|---|---|
| Data Transport | Carries desk phone fields (PhoneNumber, MacAddress) |
| UI Metadata via Attributes | Uses [Display(Name="β¦")] for friendly labels in views |
| Inheritance | Extends a base DetailViewModel for common detail fields (e.g., Id, CreatedDate, etc.) |
3. Dependencies & Technologies π οΈ
- .NET Version: .NET Framework / .NET Core (compatible with
System.ComponentModel.DataAnnotations) - Architecture: ASP.NET MVC / Razor Pages ViewModel pattern
- Libraries:
System.ComponentModel.DataAnnotationsfor validation/display metadataIrbyImaging.Models.DetailViewModel(base class)
4. DeskPhoneDetailViewModel Class Details
4.1. Source Code π₯οΈ
// ***********************************************************************
// Assembly : IrbyImaging.UI
// Author : kvamshi
// Created : 03-31-2022
// Last Modified By : kvamshi
// Last Modified On : 03-31-2022
// ***********************************************************************
// <copyright>
// file="DeskPhoneDetailViewModel.cs" company="IrbyImaging.UI">
// Copyright (c). All rights reserved.
// </copyright>
// ***********************************************************************
using System.ComponentModel.DataAnnotations;
namespace IrbyImaging.Models
{
/// <summary>
/// Represents the details of a desk phone.
/// Inherits common fields from DetailViewModel.
/// </summary>
public class DeskPhoneDetailViewModel : DetailViewModel
{
/// <summary>
/// The phone number assigned to this desk phone.
/// </summary>
[Display(Name = "Phone Number")]
public string PhoneNumber { get; set; }
/// <summary>
/// The network MAC address of the desk phone.
/// </summary>
[Display(Name = "MAC Address")]
public string MacAddress { get; set; }
}
}
4.2. Class Diagram π
classDiagram
class DetailViewModel {
<<abstract>>
+ int Id
+ DateTime CreatedDate
+ string CreatedBy
+ β¦ // other common detail fields
}
class DeskPhoneDetailViewModel {
+ string PhoneNumber
+ string MacAddress
}
DeskPhoneDetailViewModel --|> DetailViewModel
4.3. Properties & DataAnnotations π·οΈ
| Property | Type | Display Name | Description |
|---|---|---|---|
| PhoneNumber | string | Phone Number | Desk phoneβs number (e.g., 555β1234) |
| MacAddress | string | MAC Address | Unique hardware address (e.g., 00β1Aβ2B) |
5. Usage Scenarios & Examples π‘
Scenario 1: Displaying Details in Razor View
// Controller Action
public IActionResult Details(int id)
{
// 1. Fetch data from service or database
var deskPhone = _deskPhoneService.GetById(id);
// 2. Map to ViewModel
var vm = new DeskPhoneDetailViewModel {
Id = deskPhone.Id,
PhoneNumber = deskPhone.Number,
MacAddress = deskPhone.Mac,
CreatedDate = deskPhone.CreationDate,
CreatedBy = deskPhone.CreatorName
};
// 3. Return to View
return View(vm);
}
<!-- Views/DeskPhone/Details.cshtml -->
@model IrbyImaging.Models.DeskPhoneDetailViewModel
<h2>Desk Phone Details</h2>
<dl class="row">
<dt class="col-sm-3">@Html.DisplayNameFor(m => m.PhoneNumber)</dt>
<dd class="col-sm-9">@Html.DisplayFor(m => m.PhoneNumber)</dd>
<dt class="col-sm-3">@Html.DisplayNameFor(m => m.MacAddress)</dt>
<dd class="col-sm-9">@Html.DisplayFor(m => m.MacAddress)</dd>
<dt class="col-sm-3">@Html.DisplayNameFor(m => m.CreatedDate)</dt>
<dd class="col-sm-9">@Html.DisplayFor(m => m.CreatedDate)</dd>
</dl>
Scenario 2: Model Binding in a POST Edit Form
<form asp-action="Edit">
<input type="hidden" asp-for="Id" />
<div class="form-group">
<label asp-for="PhoneNumber"></label>
<input asp-for="PhoneNumber" class="form-control" />
</div>
<div class="form-group">
<label asp-for="MacAddress"></label>
<input asp-for="MacAddress" class="form-control" />
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
6. Integration in Controllers & Views π
-
Controller
- Inject services/repositories that handle
DeskPhoneentities. - Map domain model β
DeskPhoneDetailViewModel. - Return to views or APIs.
- Inject services/repositories that handle
-
View
- Use
@model DeskPhoneDetailViewModel. - Display or bind form fields with
Html.DisplayFor/Html.EditorForor Tag Helpers.
- Use
-
API
- If exposing via Web API, you can return
DeskPhoneDetailViewModelin JSON.
- If exposing via Web API, you can return
[HttpGet("api/deskphones/{id}")]
public ActionResult<DeskPhoneDetailViewModel> GetPhone(int id)
{
var phone = _service.GetById(id);
if (phone == null) return NotFound();
var vm = _mapper.Map<DeskPhoneDetailViewModel>(phone);
return Ok(vm);
}
7. Conclusion & Best Practices π―
- Single Responsibility: This ViewModel only holds whatβs needed for desk phone details.
- Reusability: By inheriting
DetailViewModel, share common detail fields across multiple models. - Separation of Concerns: Keep domain/business logic out of ViewModelsβuse them purely for data transfer to/from UI.
- Validation & Metadata: Leverage DataAnnotations (
[Display],[Required], etc.) for builtβin validation and userβfriendly labels.
With
DeskPhoneDetailViewModel, your MVC or Razor Pages application cleanly separates UI concerns from domain logic, ensures consistent metadata for forms/displays, and lays a strong foundation for maintainable, testable code. π
Happy coding! π§βπ»π