📋 Documentation: Views/Computers/Create.cshtml
🚀 Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- View Model & Data Model
- Razor Page Structure
- Form Fields & Tag Helpers
- Validation & Error Handling
- JavaScript & Third‐party Integrations
- Sample Usage & Code Snippets
- UML Diagram (Mermaid)
- Conclusion
🔍 1. Overview & Purpose
The Create.cshtml Razor view renders a form to add a new Computer asset in the IrbyImaging application.
- Path:
IrbyImaging/Views/Computers/Create.cshtml - Action: Renders on HTTP GET, posts data to
ComputersController.CreatePOST action. - Layout: Inherits from
~/Views/Shared/_ATSLayout.cshtmlfor a consistent look & feel.
Why?
- Enable end-users (e.g., IT Admins) to register new computers by capturing relevant metadata: model, serial number, purchase date, owner, status, etc.
🛠️ 2. Dependencies & Technologies
| Category | Technology / Library |
|---|---|
| .NET Framework / Core | ASP.NET Core MVC |
| Templating / Tag Helpers | Razor Syntax (@model, asp-for, asp-items, etc.) |
| Validation | Data Annotations + asp-validation-for & asp-validation-summary |
| Styling / Grid | Bootstrap 4 (Grid & Forms) |
| JavaScript Libraries | Bootstrap Bundle, MetisMenu, Simplebar, Waves.js |
| Partial Views | _ErrorModalPartial, _ValidationScriptsPartial, _ErrorModalScriptsPartial |
📋 3. Key Functional Areas
-
Model Binding
- Binds to
ComputerCreateViewModel - Populates drop‑down lists via
Model.ModelList,Model.OperatingSystemList,Model.OwnerList.
- Binds to
-
Form Generation
- Uses
<form asp-action="Create">to post to the Create endpoint. - Tag‑helpers generate labels, inputs, selects, and validation spans.
- Uses
-
Validation
- Displays field‑level errors and summary messages.
- Client‑side via JQuery Validation + Unobtrusive scripts.
-
Error Modal
- Conditionally renders
_ErrorModalPartialifModel.ErrorMessageis not empty.
- Conditionally renders
-
Third‑Party Scripts
- Enhances UX with menus, custom scrollbars, and wave effects.
🔧 4. View Model & Data Model
ComputerCreateViewModel Properties
| Property | Type | Description |
|---|---|---|
| ModelId | int | Selected Computer Model ID |
| ModelList | IEnumerable | Dropdown items for computer models |
| SerialNumber | string | Manufacturer serial number |
| AssetNumber | string | Internal asset tracking number |
| UnitNumber | string | Unit designation within an office |
| CpsNumber | string | CPS (Central Processing Service) tag number |
| OperatingSystemId | int | Selected Operating System ID |
| OperatingSystemList | IEnumerable | Dropdown items for OS options |
| PurchaseDate | DateTime | Date of purchase |
| OwnerId | int | Selected Owner/User ID |
| OwnerList | IEnumerable | Dropdown items for asset owners |
| Status | AssetStatus (enum) | Asset condition/status (Excl. “All”) |
| NeedsReview | bool | Flag for QA or dept. review |
| Notes | string | Free‑form admin notes |
| ErrorMessage | string | Server‑side error message to display in modal |
Note:
AssetStatusis an enum inIrbyImaging.Utilitywith values likeAvailable,InUse,Retired, etc.
🏗️ 5. Razor Page Structure
@model ComputerCreateViewModel
@{
ViewData["Title"] = "Create";
Layout = "~/Views/Shared/_ATSLayout.cshtml";
}
<h2>New Computer</h2>
<hr />
<div class="row">
<div class="col-md-4">
<!-- Form starts -->
<form asp-action="Create">
<!-- Validation summary -->
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<!-- ... form fields (see next section) ... -->
<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>
</form>
<!-- Form ends -->
</div>
</div>
<!-- Conditional Error Modal -->
@if (!string.IsNullOrEmpty(Model.ErrorMessage))
{
await Html.RenderPartialAsync("_ErrorModalPartial");
}
<!-- JS Includes -->
<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"); }
}
📝 6. Form Fields & Tag Helpers
| Label / Field | Tag Helper Syntax | Description |
|---|---|---|
| Model | <select asp-for="ModelId" asp-items="Model.ModelList" class="form-control" /> |
Dropdown to select a computer Model |
| Serial Number | <input asp-for="SerialNumber" class="form-control" /> |
Text input for device serial |
| Asset Number | <input asp-for="AssetNumber" class="form-control" /> |
Internal tracking number |
| Unit Number | <input asp-for="UnitNumber" class="form-control" /> |
Office/unit identifier |
| CPS Number | <input asp-for="CpsNumber" class="form-control" /> |
CPS tag |
| Operating System | <select asp-for="OperatingSystemId" asp-items="Model.OperatingSystemList" /> |
Dropdown for OS |
| Purchase Date | <input asp-for="PurchaseDate" class="form-control" /> |
Date picker input (HTML5 type=date) |
| Owner | <select asp-for="OwnerId" asp-items="Model.OwnerList" /> |
Dropdown for current owner |
| Status | <select asp-for="Status" asp-items="@(Html.GetEnumSelectList<AssetStatus>().Where(...))" /> |
Filters out “All” status |
| Needs Review | <input asp-for="NeedsReview" type="checkbox" /> |
QA / review flag |
| Notes | <textarea asp-for="Notes" class="form-control"></textarea> |
Multi‑line admin notes |
Validation Spans
Each input is accompanied by:
<span asp-validation-for="FieldName" class="text-danger"></span>
This renders field‐specific error messages when validation fails.
✅ 7. Validation & Error Handling
-
Client‐Side
- Enabled by
_ValidationScriptsPartial(JQuery Unobtrusive + JQuery Validate). - Prevents invalid submission in-browser.
- Enabled by
-
Server‐Side
- Model binding + Data Annotations on the ViewModel ensure validity.
- Error messages collected in
ModelState.
-
Error Modal
- If
Model.ErrorMessageis not empty,_ErrorModalPartialis rendered to display critical errors (e.g., DB failure).
- If
📦 8. JavaScript & Third‐party Integrations
| Library | Purpose |
|---|---|
| bootstrap.bundle.min.js | Core UI framework (modal, grid, forms, buttons) |
| metisMenu.min.js | Sidebar & nested menu management |
| simplebar.min.js | Custom scrollbars |
| waves.min.js | Ripple/wave effects on UI elements |
Scripts are included at the bottom of the page for performance.
🧩 9. Sample Usage & Code Snippets
Controller (Simplified)
public class ComputersController : Controller
{
// GET: Computers/Create
public IActionResult Create()
{
var vm = new ComputerCreateViewModel
{
ModelList = _repo.GetModelsSelectList(),
OperatingSystemList = _repo.GetOsSelectList(),
OwnerList = _repo.GetOwnersSelectList()
};
return View(vm);
}
// POST: Computers/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(ComputerCreateViewModel vm)
{
if (ModelState.IsValid)
{
try
{
await _repo.AddComputerAsync(vm);
return RedirectToAction(nameof(Index));
}
catch (Exception ex)
{
vm.ErrorMessage = ex.Message;
}
}
// Redisplay form if we got this far
vm.ModelList = _repo.GetModelsSelectList();
vm.OperatingSystemList = _repo.GetOsSelectList();
vm.OwnerList = _repo.GetOwnersSelectList();
return View(vm);
}
}
Razor Drop‑down Population
<select asp-for="OperatingSystemId"
class="form-control"
asp-items="Model.OperatingSystemList">
</select>
🔗 10. UML Diagram (Mermaid)
classDiagram
class ComputerCreateViewModel {
+int ModelId
+IEnumerable<SelectListItem> ModelList
+string SerialNumber
+string AssetNumber
+string UnitNumber
+string CpsNumber
+int OperatingSystemId
+IEnumerable<SelectListItem> OperatingSystemList
+DateTime PurchaseDate
+int OwnerId
+IEnumerable<SelectListItem> OwnerList
+AssetStatus Status
+bool NeedsReview
+string Notes
+string ErrorMessage
}
🏁 11. Conclusion
- Create.cshtml is a Razor view designed to capture all necessary properties of a Computer asset.
- It leverages ASP.NET Core tag helpers, strong‐typed view models, and client/server validation to ensure data integrity.
- Integrated third‐party scripts enhance the user experience with responsive UI components.
- Error handling is robust, utilizing both in‐line validation messages and a modal on server exceptions.
With this documentation, developers and maintainers can quickly understand and extend the Create view in the Computers section of the IrbyImaging application. 🚀