ComputerEditViewModel π₯οΈ
Namespace:
IrbyImaging.Models
Assembly:IrbyImaging.UI
Author: kvamshi
Created: 03β31β2022
Last Modified: 03β31β2022
π Table of Contents
- Overview & Purpose
- Key Functional Areas π οΈ
- Dependencies & Technologies π¦
- Class Diagram π
- View Model Properties π
- Data Annotations & Validation β
- Helper Methods & Utilities π§©
- Error Handling & Return Types π‘οΈ
- API Endpoints Overview π
- Thirdβparty Integrations π
- Sample Code Blocks π»
- Usage Examples π
- Conclusion π
1. Overview & Purpose
The ComputerEditViewModel is a ViewModel in an ASP.NET Core MVC application, used to encapsulate all the data required by the UI to create or edit a Computer entity. It inherits from a base EditViewModel (which likely provides common fields like Id, CreatedDate, etc.) and extends it with computerβspecific properties:
- Operating System selection
- Asset / Unit / CPS numbers
- Review flags
This separation of concerns (ViewModel vs. domain model) helps to keep UI and business logic loosely coupled, makes validation simpler, and prevents over-posting attacks.
2. Key Functional Areas π οΈ
- Operating System Binding
- Select lists for dropdowns
- Stronglyβtyped
OperatingSystemId
- Asset Tracking
AssetNumber,UnitNumber,CpsNumber
- Review Workflow
NeedsReviewflag to drive IT review processes
- Data Validation
- Using Data Annotations for required fields
3. Dependencies & Technologies π¦
| Dependency | Purpose |
|---|---|
Microsoft.AspNetCore.Mvc.Rendering |
SelectListItem for dropdowns in Razor views. |
System.ComponentModel.DataAnnotations |
Validation attributes like [Required]. |
IrbyImaging.Models |
Base classes and shared models. |
4. Class Diagram π
βββββββββββββββββββββββββ
β EditViewModel β
β (Base Class / UI VM) β
βββββββββββ²ββββββββββββββ
β
β inherits
β
βββββββββββββββββββββββββββββ
β ComputerEditViewModel β
βββββββββββββββββββββββββββββ
β + int OperatingSystemIdβ
β + IEnumerable<SelectListItem> OperatingSystemList β
β + string AssetNumber β
β + string UnitNumber β
β + string CpsNumber β
β + bool NeedsReview β
βββββββββββββββββββββββββββββ
5. View Model Properties π
| Property | Type | Attributes | Description |
|---|---|---|---|
| OperatingSystemId | int |
[Display(Name="Operating System")] |
Holds the selected OS ID. |
| OperatingSystemList | IEnumerable<SelectListItem> |
β | Provides the dropdown options. |
| AssetNumber | string |
[Required][Display(Name="JDE Asset Number")] |
JDE asset tracking number. |
| UnitNumber | string |
[Required][Display(Name="JDE Unit Number")] |
JDE unit identifier. |
| CpsNumber | string |
[Required][Display(Name="CPS Number")] |
CPS (computer parts) number. |
| NeedsReview | bool |
[Display(Name="Needs IT Review?")] |
Flag whether IT review is needed. |
6. Data Annotations & Validation β
-
[Required]
Ensures the user provides a value before the form can be submitted. -
[Display(Name = "...")]
Customizes the label rendered in the Razor view (e.g., form labels, validation messages).
ASP.NET Core automatically enforces these validations both clientβside (via unobtrusive validation) and serverβside.
7. Helper Methods & Utilities π§©
Note:
This class does not define any helper methods or utility functions. Its sole purpose is to serve as a data container for the UI layer.
8. Error Handling & Return Types π‘οΈ
-
Validation Errors:
- Collected in
ModelStateon form submission. - Return to the view with validation summaries/highlights.
- Collected in
-
No custom exceptions are thrown directly by this ViewModel.
9. API Endpoints Overview π
This ViewModel is not directly tied to any Web API endpoint.
Instead, it is used in MVC Controller actions:
// GET: Computer/Edit/5
public async Task<IActionResult> Edit(int id)
{
var model = await _computerService.GetForEditAsync(id);
return View(model);
}
// POST: Computer/Edit/5
[HttpPost]
public async Task<IActionResult> Edit(ComputerEditViewModel model)
{
if (!ModelState.IsValid)
return View(model);
await _computerService.UpdateAsync(model);
return RedirectToAction("Index");
}
10. Thirdβparty Integrations π
- None β only builtβin ASP.NET Core MVC components are used.
11. Sample Code Blocks π»
11.1. Setting up the OS List in the Controller
public async Task<IActionResult> Create()
{
var model = new ComputerEditViewModel
{
OperatingSystemList = await _lookupService.GetOsSelectListAsync()
};
return View(model);
}
11.2. Razor Form Snippet
@model IrbyImaging.Models.ComputerEditViewModel
<form asp-action="Create" method="post">
<div class="form-group">
<label asp-for="OperatingSystemId"></label>
<select asp-for="OperatingSystemId" asp-items="Model.OperatingSystemList" class="form-control"></select>
<span asp-validation-for="OperatingSystemId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="AssetNumber"></label>
<input asp-for="AssetNumber" class="form-control" />
<span asp-validation-for="AssetNumber" class="text-danger"></span>
</div>
<!-- Similar blocks for UnitNumber, CpsNumber, NeedsReview -->
<button type="submit" class="btn btn-primary">Save</button>
</form>
12. Usage Examples π
// In your ComputerController.cs
public class ComputerController : Controller
{
private readonly IComputerService _computerService;
private readonly ILookupService _lookupService;
public ComputerController(IComputerService computerService, ILookupService lookupService)
{
_computerService = computerService;
_lookupService = lookupService;
}
// PREPARE EDIT PAGE
public async Task<IActionResult> Edit(int id)
{
var vm = await _computerService.GetForEditAsync(id);
vm.OperatingSystemList = await _lookupService.GetOsSelectListAsync();
return View(vm);
}
// HANDLE POST
[HttpPost]
public async Task<IActionResult> Edit(ComputerEditViewModel vm)
{
if (!ModelState.IsValid)
{
vm.OperatingSystemList = await _lookupService.GetOsSelectListAsync();
return View(vm);
}
await _computerService.UpdateAsync(vm);
return RedirectToAction(nameof(Index));
}
}
13. Conclusion π
The ComputerEditViewModel is a lean, focused definition of all the fields required to edit a computer record in the UI:
- π― Clearly separates UI concerns from domain logic
- π¨ Leverages ASP.NET Coreβs dataβannotation validation
- π Simplifies the populating of dropdown lists
- π Reduces risk of overβposting by explicitly listing only the fields you want to receive
By following this pattern, youβll maintain clean, testable, and secure UI logic throughout your ASP.NET Core MVC application.