Create.cshtml

# 📄 Create.cshtml Documentation

> **Path:** `IrbyImaging/Views/CarrierType/Create.cshtml`  
> **Model:** `IrbyImaging.DomainClasses.CarrierType`

---

## 📑 Table of Contents

1. [Overview & Purpose](#overview--purpose)  
2. [Dependencies & Technologies](#dependencies--technologies)  
3. [Key Functional Areas](#key-functional-areas)  
4. [View Model & Data Model](#view-model--data-model)  
5. [Layout & Page Metadata](#layout--page-metadata)  
6. [Form Structure & Input Validation](#form-structure--input-validation)  
7. [Buttons & Navigation](#buttons--navigation)  
8. [Error Handling & Validation Feedback](#error-handling--validation-feedback)  
9. [Controller & API Interaction](#controller--api-interaction)  
10. [Sample Usage & Screenshots](#sample-usage--screenshots)  
11. [UML Diagram](#uml-diagram)  
12. [Conclusion](#conclusion)  

---

## 1. Overview & Purpose 📝

The **Create.cshtml** Razor view renders a **“New Carrier”** form that allows users to input and submit a new **CarrierType** entity.  
It integrates seamlessly with ASP.NET Core MVC’s Tag Helpers, model binding, and client‐side validation.

- **Primary Goal**: Collect a single property (`Carrier`) for a new carrier type and post it back to the `CarrierTypeController.Create` action.  
- **Usage**: Navigate to `/CarrierType/Create` in your browser to see the form.  

---

## 2. Dependencies & Technologies 🔧

| Technology / Library           | Purpose                                            |
| ------------------------------ | -------------------------------------------------- |
| ASP.NET Core MVC               | Framework for controllers & views                  |
| Razor View Engine              | `.cshtml` templating                               |
| Tag Helpers (`asp-for`, etc.)  | HTML form generation & model‐binding convenience   |
| jQuery Validation (bundled)    | Client‐side form validation                        |
| DataAnnotations (`[Required]`) | Server‐side model validation (in DomainClasses)    |
| Bootstrap 4 (CSS classes)      | Responsive layout & styling                        |

---

## 3. Key Functional Areas 🔑

| Section                    | Responsibility                                   |
| -------------------------- | ------------------------------------------------ |
| Page Metadata              | Sets title & layout                              |
| Header                     | Displays “New Carrier” heading                   |
| Validation Summary         | Shows server‐side & client errors at top         |
| Form Group (Carrier field) | Label, input & validation for `Carrier`          |
| Actions                    | “Index List” link & “Create” submit button       |

---

## 4. View Model & Data Model 📦

**Model used by the view**  
```csharp
namespace IrbyImaging.DomainClasses
{
    public class CarrierType
    {
        public int Id { get; set; }
        
        [Required(ErrorMessage = "Carrier name is required.")]
        [StringLength(100, ErrorMessage = "Max 100 characters.")]
        public string Carrier { get; set; }
    }
}
  • The view’s top line references this model:

    @model IrbyImaging.DomainClasses.CarrierType
    
  • DataAnnotations such as [Required] and [StringLength] provide validation metadata.


5. Layout & Page Metadata ⚙️

@{
    ViewData["Title"] = "Create";
    Layout = "~/Views/Shared/_ATSLayout.cshtml";
}
  • ViewData["Title"]: Sets the browser tab title to Create.
  • Layout: Inherits the shared _ATSLayout.cshtml (common header/footer, nav, scripts).

6. Form Structure & Input Validation 🏗️

<form asp-action="Create">
    <!-- Server & Model Errors -->
    <div asp-validation-summary="ModelOnly" class="text-danger"></div>

    <!-- Carrier Field Group -->
    <div class="form-group">
        <label asp-for="Carrier" class="control-label"></label>
        <input asp-for="Carrier" id="typeVal" class="form-control" />
        <span asp-validation-for="Carrier" class="text-danger"></span>
    </div>
    
    <!-- Action 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>
</form>
  • <form asp-action="Create">
    Binds to CarrierTypeController.Create(CarrierType model) (HTTP POST by default).
  • asp-validation-summary="ModelOnly"
    Displays all model‐level errors at top.
  • asp-for="Carrier"
    Generates <label> and <input> tags tied to Model.Carrier.
  • asp-validation-for="Carrier"
    Renders field‐specific validation messages.

7. Buttons & Navigation 🔗

Button Tag Helper Purpose
Index List (🔴) asp-action="Index" Returns to the list of carriers.
Create (🟢) <input type="submit"> Submits the form to save the data.
  • CSS Classes:
    • btn btn-danger irby-red → styled as a red “Cancel/Back” link
    • btn btn-success irby-green → styled as a green “Submit” button

8. Error Handling & Validation Feedback ✅ / ❌

  1. Client‐Side
    • Uses unobtrusive jQuery Validation (bundled by default in ASP.NET Core) to intercept invalid submissions.
  2. Server‐Side
    • On invalid model state, controller returns the same view and asp-validation-summary & asp-validation-for show errors.
  3. HTML Structure
    • Errors are styled with text-danger (red text).

9. Controller & API Interaction 🔄

public class CarrierTypeController : Controller
{
    // GET: /CarrierType/Create
    public IActionResult Create()
    {
        return View();
    }

    // POST: /CarrierType/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create(CarrierType model)
    {
        if (!ModelState.IsValid)
            return View(model);

        _context.CarrierTypes.Add(model);
        await _context.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
    }
}
  • GET renders the blank form.
  • POST receives the bound CarrierType model, checks ModelState, and on success persists to database and redirects to Index.

10. Sample Usage & Screenshots 📷

  1. Navigate
    https://<your-app>/CarrierType/Create
  2. Fill Form
    • Enter a carrier name (e.g., “FedEx”).
  3. Submit
    • Click Create → If valid, redirected to /CarrierType/Index.
  4. Errors
    • Leave blank → inline and summary errors appear in red.
_______________________
| New Carrier         |
| ------------------- |
|   [Carrier]         |
|   *Carrier is required*
| [Index List] [Create]

11. UML Diagram 📊

+----------------------+           +------------------------------+
| CarrierTypeController|           |     CarrierType (Model)      |
+----------------------+           +------------------------------+
| + Create() : View    | <-------> | - int Id                     |
| + Create(model)      |           | - string Carrier             |
+----------------------+           +------------------------------+
           |
           | renders
           v
+------------------------------+
| Create.cshtml (Razor View)   |
+------------------------------+
| - @model CarrierType         |
| - <form asp-action="Create"> |
+------------------------------+

12. Conclusion 🎯

The Create.cshtml view is a concise, well‐structured Razor page for collecting a new CarrierType. Thanks to ASP.NET Core’s Tag Helpers and DataAnnotations, it offers strong model binding, server‐ and client‐side validation, and a consistent UI by leveraging a shared layout and Bootstrap classes.

🔍 Next Steps

  • Implement additional fields (e.g., Description, Status) by extending CarrierType.
  • Enhance UX with custom JavaScript or AJAX.
  • Write integration tests for the Create workflow.

Happy coding! 🚀