📄 IrbyImaging/Views/AssetModels/Create.cshtml Documentation


📑 Table of Contents

  1. Overview & Purpose
  2. File Location & Layout
  3. Dependencies & Technologies
  4. Model Binding & Data Models
  5. View Structure & Key Functional Areas
  6. Client‑Side Interactivity (jQuery)
  7. Validation & Error Handling
  8. Usage Example & Workflow
  9. UML Sequence Diagram
  10. Conclusion

🔍 1. Overview & Purpose

This Razor View renders a form allowing users to create a new AssetModel in the IrbyImaging application.

  • 🎯 Goal: Capture Category, Device Type (if applicable), Make, and Name of a new asset model.
  • 🚀 When used: Invoked by the AssetModelsController.Create GET action, and posts back to the same Create action.

📁 2. File Location & Layout

IrbyImaging/
 └─ Views/
     └─ AssetModels/
         └─ Create.cshtml
  • Layout: Uses the shared layout ~/Views/Shared/_ATSLayout.cshtml
  • Title: Sets ViewData["Title"] = "Create"

🛠️ 3. Dependencies & Technologies

Technology Usage
ASP.NET Core MVC Razor view engine, tag helpers (asp-for, asp-action, etc.)
jQuery Simple client‑side interactivity
Bootstrap (CSS) Styling: .row, .col-md-4, .form-control, .btn
DomainClasses IrbyImaging.DomainClasses.AssetModel (model for binding)
Utility Enums IrbyImaging.Utility.AssetCategory & DeviceType enums

📦 4. Model Binding & Data Models

@model IrbyImaging.DomainClasses.AssetModel
  • The view is strongly typed to AssetModel.
  • Properties used:
    • Category (enum)
    • Type (enum)
    • MakeId (int)
    • Name (string)

🖥️ 5. View Structure & Key Functional Areas

<h2>New Model</h2>
<hr />
<div class="row">
  <div class="col-md-4">
    <form asp-action="Create">
      <!-- Validation Summary -->
      <!-- Category -->
      <!-- Device Type (conditional) -->
      <!-- Make -->
      <!-- Name -->
      <!-- Actions -->
    </form>
  </div>
</div>

5.1 Category Dropdown

<div class="form-group">
  <label asp-for="Category" class="control-label"></label>
  <select asp-for="Category"
          class="form-control"
          asp-items="@Html.GetEnumSelectList<IrbyImaging.Utility.AssetCategory>().OrderBy(e => e.Text)"
          id="cat">
  </select>
  <span asp-validation-for="Category" class="text-danger"></span>
</div>
  • Populates from the AssetCategory enum.
  • Sorted by display text.
  • ID: cat – used in the script to trigger conditional UI.

5.2 Device Type Dropdown (Conditional)

<div class="form-group" id="typeDiv" style="display:none">
  <label asp-for="Type" class="control-label"></label>
  <select asp-for="Type"
          id="typeVal"
          class="form-control"
          asp-items="@Html.GetEnumSelectList<IrbyImaging.Utility.DeviceType>()">
  </select>
  <span asp-validation-for="Type" class="text-danger"></span>
</div>
  • Hidden by default (style="display:none").
  • Revealed only when Category == MobileDevice.

5.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>
  • Populated via ViewBag.MakeId from the controller.
  • Allows selection of a Manufacturer or Make.

5.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>
  • Simple text box for the model’s name.
  • Displays validation messages.

5.5 Form Actions & Buttons

<div class="form-group">
  <a asp-action="Index" class="btn btn-danger irby-red">Index List</a>
  &nbsp;&nbsp;
  <input type="submit" value="Create" class="btn btn-success irby-green" />
</div>
  • Index List: Returns to listing view (GET /AssetModels/Index).
  • Create: Submits the form via POST to AssetModelsController.Create.

🧩 6. Client‑Side Interactivity (jQuery)

@section Scripts {
  @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
  <script>
    $(document).ready(function () {
      $("#cat").on("click", () => {
        if ($("#cat").val() == @((int)IrbyImaging.Utility.AssetCategory.MobileDevice)) {
          $("#typeDiv").toggle();            // Show/hide Type dropdown
        } else {
          $("#typeVal").val("");             // Reset selection
          $("#typeDiv").hide();
        }
      });
    });
  </script>
}
  • 👆 On Category Click:
    • Checks if the selected value equals the integer value of AssetCategory.MobileDevice.
    • Toggles the #typeDiv container.
    • Resets the #typeVal when not a mobile device.

🚨 7. Validation & Error Handling

  • Server‑Side: ModelState validation in controller triggers asp-validation-summary and asp-validation-for.
  • Client‑Side: _ValidationScriptsPartial adds unobtrusive jQuery validation.
Area HTML Helper Purpose
Summary of model errors <div asp-validation-summary="ModelOnly"> Lists all model errors at top.
Field‑level error message <span asp-validation-for="..."> Shows specific field error.

🛠️ 8. Usage Example & Workflow

  1. User navigates to /AssetModels/Create (GET).
  2. Controller returns the Create.cshtml view.
  3. User selects:
    • Category (e.g., MobileDevice)
    • (If MobileDevice): selects Type
    • Make from dropdown
    • Enters Name
  4. Client displays/hides Type based on selection.
  5. Submit → POST /AssetModels/Create
  6. Controller validates the model:
    • Valid → saves to database & redirects to Index
    • Invalid → redisplay form with errors

📈 9. UML Sequence Diagram

sequenceDiagram
    participant U as User
    participant B as Browser
    participant C as Controller
    participant V as View

    U->>B: GET /AssetModels/Create
    B->>C: Request Create()
    C->>V: Return Create.cshtml (model: AssetModel)
    V->>B: Render form
    U->>B: Fill fields & click Create
    B->>C: POST /AssetModels/Create (form data)
    alt ModelState.IsValid
        C->>DB: Save new AssetModel
        C->>B: Redirect to /AssetModels/Index
    else Invalid
        C->>V: Return Create.cshtml (with ModelState errors)
        V->>B: Render errors
    end

🏁 10. Conclusion

  • Create.cshtml is the entry point for adding new asset models.
  • Combines Razor server‑side binding with jQuery for a smooth user experience.
  • Extensible: Easily add new categories, types, or validation rules.
  • Leverages ASP.NET Core MVC features: Tag Helpers, Layouts, Partial Views, and Enums.

Happy Coding! 🌟 If you have questions, feel free to explore the controller actions or the shared layout to see how data flows throughout IrbyImaging.