📝 Documentation: IrbyImaging/Views/AssetModels/Edit.cshtml
A detailed walkthrough of the Edit Razor view for Asset Models in the IrbyImaging application.
📑 Table of Contents
- Overview & Purpose
- Layout & ViewData
- Model & ViewModel
- Form Structure
4.1. Category Selection
4.2. Device Type Selection
4.3. Make Dropdown
4.4. Name Input
4.5. Availability Toggle
4.6. Action Buttons - Validation
- Client-Side Behavior
- Dependencies & Technologies
- Sample Usage & Controller Snippet
- UML Diagram: AssetModel
- Conclusion
1. Overview & Purpose
This Razor view renders a form for editing an existing AssetModel entity. It allows administrators to:
- Select a Category (e.g., MobileDevice, Computer)
- Pick a Device Type (only visible for Mobile Devices)
- Choose a Make from a dropdown
- Edit the Name of the model
- Toggle its availability
- Submit updates or return to the list
It integrates server‑side validation, uses ASP.NET Core Tag Helpers, and includes lightweight jQuery for dynamic behavior. 🚀
2. Layout & ViewData
@{
ViewData["Title"] = "Edit";
Layout = "~/Views/Shared/_ATSLayout.cshtml";
}
- Title: Sets the HTML
<title>to “Edit.” - Layout: Reuses the shared layout
_ATSLayout.cshtmlfor consistent branding/navigation.
3. Model & ViewModel
@model IrbyImaging.DomainClasses.AssetModel
- Model Type:
AssetModel - Namespace:
IrbyImaging.DomainClasses
Key Properties (full class in UML below):
| Property | Type | Description |
|---|---|---|
Category |
AssetCategory (enum) |
General category of the asset. |
Type |
DeviceType (enum) |
Specific device type (phone, tablet, etc.). |
MakeId |
int |
Foreign key to a Make entity. |
Name |
string |
Descriptive name of the model. |
IsAvailableOption |
bool |
Flag: is this option currently available? |
4. Form Structure
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<!-- 4.1 - 4.6 form groups here -->
</form>
4.1 Category Selection
<div class="form-group">
<label asp-for="Category" class="control-label"></label>
<select asp-for="Category" class="form-control"
asp-items="@Html.GetEnumSelectList<AssetCategory>()"
id="cat"></select>
<span asp-validation-for="Category" class="text-danger"></span>
</div>
- asp-items: Populates options from the
AssetCategoryenum. - id="cat": Used by client‑side script to toggle the Device Type field.
4.2 Device Type Selection
<div class="form-group" id="typeDiv">
<label asp-for="Type" class="control-label"></label>
<select asp-for="Type" id="typeVal" class="form-control"
asp-items="@Html.GetEnumSelectList<DeviceType>()"></select>
<span asp-validation-for="Type" class="text-danger"></span>
</div>
- Visible only when
Category == MobileDevice. - Toggled via jQuery (see section 6).
4.3 Make Dropdown
<div class="form-group">
<label asp-for="Make" class="control-label"></label>
<select asp-for="MakeId" class="form-control"
asp-items="ViewBag.MakeId"></select>
</div>
- ViewBag.MakeId: Populated by the controller action with a
SelectListof Makes.
4.4 Name Input
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
- Standard text input for the model name.
4.5 Availability Toggle
<div class="form-group">
<label asp-for="IsAvailableOption" class="control-label">
Make this option available?
</label>
<input asp-for="IsAvailableOption" class="checkbox-inline" />
<span asp-validation-for="IsAvailableOption"
class="text-danger"></span>
</div>
- Checkbox indicating whether this asset model is active.
4.6 Action Buttons
<div class="form-group">
<a asp-action="Index" class="btn btn-danger irby-red">Index List</a>
<input type="submit" value="Update"
class="btn btn-success irby-green" />
</div>
- Index List: Navigates back without saving.
- Update: Posts form to
AssetModelsController.Edit(POST).
5. Validation
- Server‑side: TagHelper
asp-validation-summary="ModelOnly"&asp-validation-for. - Client‑side:
_ValidationScriptsPartialrenders the jQuery Validation scripts.
@section Scripts {
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}
6. Client-Side Behavior 🛠️
<script>
$(document).ready(function () {
// Initially hide Type if not MobileDevice
if ($("#cat").val() != @((int)AssetCategory.MobileDevice)) {
$("#typeDiv").hide();
}
// Toggle Type field on Category click
$("#cat").on("click", (e) => {
if ($("#cat").val() == @((int)AssetCategory.MobileDevice)) {
$("#typeDiv").toggle();
} else {
$("#typeVal").val("");
}
});
});
</script>
- Purpose: Only Mobile devices have a
Type. - Logic:
- On page load, hide the
#typeDivif the selected Category ≠MobileDevice. - On clicking the Category dropdown, show/hide the Device Type accordingly, resetting it if hidden.
- On page load, hide the
7. Dependencies & Technologies
| Level | Items |
|---|---|
| Framework | ASP.NET Core MVC (Razor Pages) |
| UI | Bootstrap 4 (CSS classes: form-control, etc.) |
| Scripts | jQuery, jQuery Validation |
| Tag Helpers | asp-for, asp-items, asp-action, etc. |
| Custom Utilities | Html.GetEnumSelectList<T>() |
| Layout | _ATSLayout.cshtml |
8. Sample Usage & Controller Snippet
// GET: AssetModels/Edit/5
public IActionResult Edit(int id) {
var model = _context.AssetModels.Find(id);
ViewBag.MakeId = new SelectList(_context.Makes, "Id", "Name");
return View(model);
}
// POST: AssetModels/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(AssetModel model) {
if (ModelState.IsValid) {
_context.Update(model);
_context.SaveChanges();
return RedirectToAction(nameof(Index));
}
ViewBag.MakeId = new SelectList(_context.Makes, "Id", "Name", model.MakeId);
return View(model);
}
Usage:
- Navigate to
/AssetModels/Edit/{id} - Form pre‑populates with existing values.
- Make changes and click Update.
- On success, returns to the Index list.
9. UML Diagram: AssetModel
+---------------------------------+
| AssetModel 📦 |
+---------------------------------+
| - Category: AssetCategory |
| - Type: DeviceType |
| - MakeId: int |
| - Name: string |
| - IsAvailableOption: bool |
+---------------------------------+
| + Edit() |
| + Validate() |
+---------------------------------+
- Enums:
AssetCategory{ MobileDevice = 0, Computer = 1, … }DeviceType{ Phone = 0, Tablet = 1, … }
10. Conclusion
This Edit view provides a streamlined UI for updating asset models within the IrbyImaging system. It leverages:
- ASP.NET Core Tag Helpers for concise Razor syntax
- Enum SelectLists for strongly‑typed dropdowns
- Client‑side scripting to improve UX (show/hide fields)
- Server & client validation to ensure data integrity
By understanding each section above, you can customize, extend, or reuse this pattern for similar edit forms in your application. 🎉
Happy Coding! 🚀