📝 Documentation: IrbyImaging/Views/AssetModels/Edit.cshtml

A detailed walkthrough of the Edit Razor view for Asset Models in the IrbyImaging application.


📑 Table of Contents

  1. Overview & Purpose
  2. Layout & ViewData
  3. Model & ViewModel
  4. 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
  5. Validation
  6. Client-Side Behavior
  7. Dependencies & Technologies
  8. Sample Usage & Controller Snippet
  9. UML Diagram: AssetModel
  10. 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.cshtml for 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 AssetCategory enum.
  • 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 SelectList of 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?&nbsp;&nbsp;
  </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>
  &nbsp;&nbsp;
  <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: _ValidationScriptsPartial renders 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 #typeDiv if the selected Category ≠ MobileDevice.
    • On clicking the Category dropdown, show/hide the Device Type accordingly, resetting it if hidden.

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:

  1. Navigate to /AssetModels/Edit/{id}
  2. Form pre‑populates with existing values.
  3. Make changes and click Update.
  4. 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! 🚀