π Edit Computer View Documentation (Edit.cshtml)
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- View Model & Data Models
- Key Components & Tag Helpers
- Form Fields Breakdown
- Validation & Error Handling
- Scripts & Partial Views
- Controller Interaction & API Endpoints
- UML Diagrams
- Usage Examples
- Conclusion
1. π Overview & Purpose
The Edit.cshtml view under IrbyImaging/Views/Computers/ is a Razor page used to render and process the βUpdate Computerβ form in an ASP.NET Core MVC application.
- Intended Use: Allows administrators or power users to modify existing computer assets (serial numbers, status, owner, notes, etc.).
- Layout: Inherits from
~/Views/Shared/_ATSLayout.cshtmlto maintain consistent look-and-feel. - Model Binding: Strongly-typed to
ComputerEditViewModel, ensuring compile-time safety.
2. π§ Dependencies & Technologies
| Technology / Library | Role |
|---|---|
| ASP.NET Core MVC | MVC framework & Tag Helpers |
| Razor (cshtml) | Server-side templating |
| Bootstrap 5 | Responsive styling & layout |
| MetisMenu, Simplebar, Waves | UI enhancements & interactive components |
| jQuery Validation | Client-side form validation (via _ValidationScriptsPartial) |
| Partial Views | Reusable error modal (_ErrorModalPartial) |
3. π§© View Model & Data Models
ComputerEditViewModel
public class ComputerEditViewModel
{
public int Id { get; set; }
public int ModelId { get; set; }
public IEnumerable<SelectListItem> ModelList { get; set; }
public string SerialNumber { get; set; }
public string AssetNumber { get; set; }
public string UnitNumber { get; set; }
public string CpsNumber { get; set; }
public int OperatingSystemId { get; set; }
public IEnumerable<SelectListItem> OperatingSystemList { get; set; }
public DateTime PurchaseDate { get; set; }
public int OwnerId { get; set; }
public IEnumerable<SelectListItem> OwnerList { get; set; }
public AssetStatus Status { get; set; }
public bool NeedsReview { get; set; }
public string Notes { get; set; }
public string Reason { get; set; }
public string ErrorMessage { get; set; }
}
AssetStatus Enum (Excerpt)
public enum AssetStatus
{
All,
Active,
Inactive,
Retired,
UnderMaintenance
}
4. β¨ Key Components & Tag Helpers
| Element | Tag Helper / Attribute | Purpose |
|---|---|---|
<form> |
asp-action="EditComputer" | Posts form data to EditComputer endpoint |
<input> / <select> / <textarea> |
asp-for | Binds form control to model property |
<select> |
asp-items | Populates dropdown via Model.*List |
| Validation messages | asp-validation-summary / asp-validation-for | Displays server- & client-side validation errors |
5. π₯οΈ Form Fields Breakdown
| Label | Control Type | Bound Property | Description |
|---|---|---|---|
| Model | Select | ModelId / ModelList |
Choose computer model from pre-loaded list |
| Serial Number | Text | SerialNumber |
Unique identifier from manufacturer |
| Asset Number | Text | AssetNumber |
Company-assigned asset tag |
| Unit Number | Text | UnitNumber |
Internal unit reference |
| CPS Number | Text | CpsNumber |
Service tracking code |
| Operating System | Select | OperatingSystemId / OperatingSystemList |
OS selection dropdown |
| Purchase Date | Date/Text | PurchaseDate |
Date of acquisition |
| Owner | Select | OwnerId / OwnerList |
Select current owner from employee list |
| Status | Select | Status |
Enum-driven statuses (excludes βAllβ) |
| Needs Review | Checkbox | NeedsReview |
Flag if asset requires review |
| Notes | Textarea | Notes |
Free-form notes and remarks |
| Reason | Textarea | Reason |
Reason for update/change |
6. β οΈ Validation & Error Handling
-
Server-side:
- The
asp-validation-summary="ModelOnly"placeholder displays model-level errors. - Each field has
asp-validation-forto show property-level errors.
- The
-
Client-side:
_ValidationScriptsPartialinjects jQuery Validate & Unobtrusive scripts.- Real-time input validation before submission.
-
Error Modal:
- If
Model.ErrorMessageis not empty, the view renders_ErrorModalPartialto show a Bootstrap modal with the message.
- If
@if (!string.IsNullOrEmpty(Model.ErrorMessage)) {
await Html.RenderPartialAsync("_ErrorModalPartial");
}
7. π οΈ Scripts & Partial Views
<!-- Core JS Libraries -->
<script src="~/libs/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="~/libs/metismenu/metisMenu.min.js"></script>
<script src="~/libs/simplebar/simplebar.min.js"></script>
<script src="~/libs/node-waves/waves.min.js"></script>
@section Scripts {
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
@{ await Html.RenderPartialAsync("_ErrorModalScriptsPartial"); }
}
- _ValidationScriptsPartial: Enables unobtrusive and client validation.
- _ErrorModalScriptsPartial: Handles modal open/close logic for error messages.
8. π Controller Interaction & API Endpoints
GET: Edit (Display Form)
public IActionResult Edit(int id)
{
var vm = _computerService.GetEditModel(id);
return View(vm);
}
- Path:
/Computers/Edit/{id} - Function: Populates
ComputerEditViewModeland rendersEdit.cshtml.
POST: EditComputer (Process Submission)
[HttpPost]
public IActionResult EditComputer(ComputerEditViewModel model)
{
if (!ModelState.IsValid)
{
// Re-populate dropdowns...
return View("Edit", model);
}
var result = _computerService.Update(model);
if (!result.Success)
{
model.ErrorMessage = result.ErrorMessage;
return View("Edit", model);
}
return RedirectToAction("Index");
}
- Path:
/Computers/EditComputer - Behavior: Validates & updates the computer record, handles errors, and redirects back to index.
9. π UML Diagrams
Class Diagram
@startuml
class ComputerEditViewModel {
+int Id
+int ModelId
+List<SelectListItem> ModelList
+string SerialNumber
+string AssetNumber
+string UnitNumber
+string CpsNumber
+int OperatingSystemId
+List<SelectListItem> OperatingSystemList
+DateTime PurchaseDate
+int OwnerId
+List<SelectListItem> OwnerList
+AssetStatus Status
+bool NeedsReview
+string Notes
+string Reason
+string ErrorMessage
}
class ComputersController {
+IActionResult Edit(int id)
+IActionResult EditComputer(ComputerEditViewModel vm)
}
ComputerEditViewModel <|-- Edit.cshtml
ComputersController ..> ComputerEditViewModel
@enduml
Sequence Diagram
@startuml
actor User
User -> Browser: Click βEditβ link
Browser -> ComputersController : GET /Computers/Edit/{id}
ComputersController -> Database: Fetch asset + lists
Database --> ComputersController: Return data
ComputersController -> Browser: Render Edit.cshtml(vm)
Browser -> User: Display form
User -> Browser: Submit βUpdateβ
Browser -> ComputersController: POST /Computers/EditComputer(vm)
ComputersController -> Service: Validate & Update
Service --> ComputersController: Result
alt Success
ComputersController -> Browser: Redirect to Index
else Failure
ComputersController -> Browser: Render Edit.cshtml(with errors)
end
@enduml
10. π Usage Examples
Rendering the Edit Form
// In ComputersController
public IActionResult Edit(int id)
{
var vm = _computerService.GetEditModel(id);
return View(vm); // Renders Edit.cshtml
}
Handling Form Submission
[HttpPost]
public IActionResult EditComputer(ComputerEditViewModel model)
{
if (!ModelState.IsValid)
{
model = _computerService.RefreshLists(model);
return View("Edit", model);
}
var updateResult = _computerService.Update(model);
if (!updateResult.Success)
{
model.ErrorMessage = updateResult.ErrorMessage;
model = _computerService.RefreshLists(model);
return View("Edit", model);
}
return RedirectToAction("Index");
}
11. π― Conclusion
The Edit.cshtml view is a critical part of the Computers management UI in IrbyImaging. It:
- Provides a full-featured form for updating computer assets.
- Leverages ASP.NET Core Tag Helpers for clean, strongly-typed markup.
- Integrates client/server validation, error modal, and partial views for a robust user experience.
- Connects seamlessly to the ComputersController endpoints for GET/POST operations.
By adhering to this structure, the application ensures consistency, maintainability, and ease of extension for future asset management requirements.
Happy coding! π₯οΈβ¨