📄 IrbyImaging/Views/AssetModels/Create.cshtml Documentation
📑 Table of Contents
- Overview & Purpose
- File Location & Layout
- Dependencies & Technologies
- Model Binding & Data Models
- View Structure & Key Functional Areas
- 5.1 Category Dropdown
- 5.2 Device Type Dropdown (Conditional)
- 5.3 Make Dropdown
- 5.4 Name Input
- 5.5 Form Actions & Buttons
- Client‑Side Interactivity (jQuery)
- Validation & Error Handling
- Usage Example & Workflow
- UML Sequence Diagram
- 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.CreateGET 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
AssetCategoryenum. - 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.MakeIdfrom 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>
<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
#typeDivcontainer. - Resets the
#typeValwhen not a mobile device.
- Checks if the selected value equals the integer value of
🚨 7. Validation & Error Handling
- Server‑Side: ModelState validation in controller triggers
asp-validation-summaryandasp-validation-for. - Client‑Side:
_ValidationScriptsPartialadds 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
- User navigates to
/AssetModels/Create(GET). - Controller returns the
Create.cshtmlview. - User selects:
- Category (e.g., MobileDevice)
- (If MobileDevice): selects Type
- Make from dropdown
- Enters Name
- Client displays/hides
Typebased on selection. - Submit → POST
/AssetModels/Create - 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.