π NetworkEquipmentEditViewModel Documentation
ποΈ Table of Contents
- Overview & Purpose
- Context & Architectural Role
- Class Diagram (UML)
- Key Functional Areas
- Dependencies & Technologies
- Code Walkthrough
- Usage Example
- Integration in MVC Flow
- Extending the ViewModel
- Conclusion
1. Overview & Purpose
File: IrbyImaging/Models/NetworkEquipment/NetworkEquipmentEditViewModel.cs
Assembly: IrbyImaging.UI
Author: kvamshi
Created / Last Modified: 03-31-2022
NetworkEquipmentEditViewModel is a ViewModel used in the IrbyImaging web application, specifically for editing Network Equipment entities within an MVC-based UI. It inherits all basic editing functionality from the shared
EditViewModelbase class and serves as a stronglyβtyped model for your βEdit Network Equipmentβ views.
2. Context & Architectural Role
In an ASP.NET MVC (or Razor Pages) application, ViewModels encapsulate data and validation logic required by your views. They sit between:
- Controllers (handling HTTP requests)
- Domain / Data Models (representing persistence)
- Razor Views (rendering HTML)
This particular ViewModel:
- Carries network equipment data to/from the Edit view.
- Provides clientβside and serverβside validation via Data Annotations (inherited).
- Keeps UIβspecific logic out of your domain entities.
3. Class Diagram (UML)
ββββββββββββββββββββββββββββββββββ
β EditViewModel ββββββββββββββ
ββββββββββββββββββββββββββββββββββ β inherits
β + Id: Guid β β
β + CreatedBy: string β β
β + CreatedDate: DateTime β β
β + ModifiedBy: string β β
β + ModifiedDate: DateTime β β
β + IsActive: bool β β
β + Validate(): IEnumerable β β
ββββββββββββββββββββββββββββββββββ β
β² inherits βββββββββββββββββββββββββββββββββββββ
β β NetworkEquipmentEditViewModel β
β βββββββββββββββββββββββββββββββββββββ
β β (no additional properties yet) β
βββββββββββββββββββββββββββββββββββ€ β
βββββββββββββββββββββββββββββββββββββ
4. Key Functional Areas
| Area | Description |
|---|---|
| Inheritance | Extends EditViewModel to reuse common editing fields and validation. |
| Scaffold Placeholder | Currently emptyβserves as a spot to add NetworkEquipmentβspecific edit properties in the future. |
| Validation | Leverages Data Annotation attributes on base class for requiredβfield checks. |
| UI Binding | Provides strong typing for Razor views (@model NetworkEquipmentEditViewModel). |
5. Dependencies & Technologies
- ASP.NET Core MVC / Razor
- System.ComponentModel.DataAnnotations (inherited for validation)
- IrbyImaging.Models namespace
- EditViewModel base class
namespace IrbyImaging.Models
{
public abstract class EditViewModel
{
[Required]
public Guid Id { get; set; }
public DateTime CreatedDate { get; set; }
public string CreatedBy { get; set; }
public DateTime? ModifiedDate { get; set; }
public string ModifiedBy { get; set; }
public bool IsActive { get; set; } = true;
}
}
6. Code Walkthrough
// NetworkEquipmentEditViewModel.cs
namespace IrbyImaging.Models
{
/// <summary>
/// ViewModel for editing a Network Equipment entity.
/// Inherits from EditViewModel for common fields.
/// </summary>
public class NetworkEquipmentEditViewModel : EditViewModel
{
// π§ Add networkβequipment-specific fields here.
// e.g. public string Hostname { get; set; }
// [Display(Name="IP Address")]
// [RegularExpression(...)]
// public string IPAddress { get; set; }
}
}
- Namespace: Groups this class under the
IrbyImaging.Modelsassembly. - XML Documentation:
<summary>and<seealso>tags help generate API docs. - Class Signature:
public class NetworkEquipmentEditViewModel : EditViewModel
7. Usage Example
7.1. Controller Action
public class NetworkEquipmentController : Controller
{
// GET: /NetworkEquipment/Edit/{id}
public IActionResult Edit(Guid id)
{
// Fetch domain model from DB/service
var domainModel = _networkEquipmentService.GetById(id);
// Map to ViewModel (using AutoMapper or manual mapping)
var vm = new NetworkEquipmentEditViewModel
{
Id = domainModel.Id,
CreatedDate = domainModel.CreatedDate,
CreatedBy = domainModel.CreatedBy,
ModifiedDate = domainModel.ModifiedDate,
ModifiedBy = domainModel.ModifiedBy,
IsActive = domainModel.IsActive
// TODO: Map network equipment fields
};
return View(vm);
}
[HttpPost]
public IActionResult Edit(NetworkEquipmentEditViewModel vm)
{
if (!ModelState.IsValid)
{
return View(vm);
}
// Map ViewModel back to domain model
var domainModel = new NetworkEquipment
{
Id = vm.Id,
// ...
};
_networkEquipmentService.Update(domainModel);
return RedirectToAction("Index");
}
}
7.2. Razor View Snippet
@model IrbyImaging.Models.NetworkEquipmentEditViewModel
<h2>Edit Network Equipment</h2>
<form asp-action="Edit" method="post">
<input asp-for="Id" type="hidden" />
<div class="form-group">
<label asp-for="IsActive"></label>
<input asp-for="IsActive" class="form-control" />
</div>
<!-- π§ Add your custom fields here -->
<!-- e.g.
<div class="form-group">
<label asp-for="IPAddress"></label>
<input asp-for="IPAddress" class="form-control" />
<span asp-validation-for="IPAddress" class="text-danger"></span>
</div>
-->
<button type="submit" class="btn btn-primary">Save</button>
</form>
8. Integration in MVC Flow
-
Routing
GET /NetworkEquipment/Edit/{id}to populate form.POST /NetworkEquipment/Editto submit updates.
-
Model Binding & Validation
- ASP.NET automatically binds form fields to the
NetworkEquipmentEditViewModel. - DataAnnotations on
EditViewModelenforce required fields, etc.
- ASP.NET automatically binds form fields to the
-
Mapping
- Use AutoMapper (or manual mapping) in Controllers to convert between domain models and view models.
-
Error Handling
ModelState.IsValidguards against invalid input.- Display validation messages in Razor with
asp-validation-for.
9. Extending the ViewModel
As your appβs needs grow, youβll likely add properties such as:
| Property Name | Type | Annotations | Purpose |
|---|---|---|---|
| Hostname | string | [Required], [StringLength(100)] | Equipment hostname |
| IPAddress | string | [RegularExpression(...)] | Validates IPv4/IPv6 format |
| Location | string | [Display(Name="Rack/Room")] | Where the device is physically located |
| PurchaseDate | DateTime | [DataType(DataType.Date)] | Acquisition date |
β Tip: Always annotate fields with [Display(Name="β¦")] and validation attributes for a better UX.
10. Conclusion
π― NetworkEquipmentEditViewModel is a lightweight, extendable ViewModel designed to streamline the Edit experience for network equipment records in the IrbyImaging application. By inheriting from a shared base class, it promotes code reuse, consistency, and maintainability.
Feel free to extend it with custom properties, wire it up with your controller logic, and leverage Razorβs tag helpers for rapid UI development!
Happy Coding! π