📄 IrbyFormsView.cs Documentation


Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. Class Definition & Properties
  5. UML Class Diagram
  6. Typical Usage
  7. Validation & Error Handling
  8. Sample Code Blocks
  9. Conclusion

1. Overview & Purpose

The IrbyFormsView class is a ViewModel in the IrbyImaging.UI.Models namespace.
It encapsulates all necessary data for uploading and displaying form files within the Irby Imaging web application UI.

  • 🎯 Primary goal: Transfer form metadata and file content between the View (Razor) and Controller.
  • 🔑 Use-case: Handles file selection (using IFormFile), and stores related information such as form number, file name, and storage path.

2. Dependencies & Technologies

Dependency Purpose
Microsoft.AspNetCore.Http Provides the IFormFile interface for file uploads.
.NET 6+ / ASP.NET Core Web framework hosting the MVC/Web API application.

3. Key Functional Areas

  • View Model Definition
    • Maps the form-related properties used in Razor Views and Controllers.
  • File Upload Handling
    • Encapsulates the uploaded file (IFormFile) along with metadata.
  • Metadata Tracking
    • Maintains FormNumber, FileName, and FilePath for storage and display.

4. Class Definition & Properties

namespace IrbyImaging.UI.Models
{
    /// <summary>
    /// Encapsulates form upload fields for the UI.
    /// </summary>
    public class IrbyFormsView
    {
        /// <summary>
        /// Unique identifier for the form.
        /// </summary>
        public string FormNumber { get; set; }

        /// <summary>
        /// Original file name of the uploaded form.
        /// </summary>
        public string FileName { get; set; }

        /// <summary>
        /// Physical or virtual path where the form is stored.
        /// </summary>
        public string FilePath { get; set; }

        /// <summary>
        /// Uploaded file payload.
        /// </summary>
        public IFormFile File { get; set; }
    }
}
Property Type Description
FormNumber string Identifier or code for the form.
FileName string Name of the uploaded file (with extension).
FilePath string Location on server or cloud where file is saved.
File IFormFile Represents the selected file in the HTTP request.

5. UML Class Diagram

classDiagram
    class IrbyFormsView {
        +string FormNumber
        +string FileName
        +string FilePath
        +IFormFile File
    }
    IFormFile <|-- IrbyFormsView : uses

6. Typical Usage

Controller Example

using Microsoft.AspNetCore.Mvc;
using IrbyImaging.UI.Models;
using System.IO;
using System.Threading.Tasks;

public class FormsController : Controller
{
    [HttpGet]
    public IActionResult UploadForm()
    {
        return View(new IrbyFormsView());
    }

    [HttpPost]
    public async Task<IActionResult> UploadForm(IrbyFormsView model)
    {
        if (!ModelState.IsValid)
            return View(model);

        if (model.File != null && model.File.Length > 0)
        {
            // Compose a unique file path
            var uploads = Path.Combine("wwwroot", "forms");
            var filePath = Path.Combine(uploads, model.File.FileName);

            // Save file to disk
            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await model.File.CopyToAsync(stream);
            }

            model.FileName = model.File.FileName;
            model.FilePath = filePath;
        }

        // TODO: Save metadata (model.FormNumber, model.FileName, model.FilePath) to DB

        return RedirectToAction("UploadConfirmation", model);
    }
}

Razor View Example

@model IrbyImaging.UI.Models.IrbyFormsView

<form asp-action="UploadForm" method="post" enctype="multipart/form-data">
    <div>
        <label>Form Number</label>
        <input asp-for="FormNumber" class="form-control" />
    </div>
    <div>
        <label>Select File</label>
        <input asp-for="File" type="file" class="form-control-file" />
    </div>
    <button type="submit" class="btn btn-primary">Upload</button>
</form>

7. Validation & Error Handling

  • ModelState Validation
    • Ensure FormNumber is provided (add [Required] if needed).
    • Ensure file size and type constraints (e.g., PDF only).
  • File I/O Exceptions
    • Wrap disk operations in try-catch to capture IOException.
  • Security
    • Sanitize file names to avoid path traversal attacks.
    • Limit accepted file extensions.

8. Sample Code Blocks

// Adding validation attributes
public class IrbyFormsView
{
    [Required(ErrorMessage = "Form Number is required.")]
    public string FormNumber { get; set; }

    [Required(ErrorMessage = "Please select a file.")]
    [DataType(DataType.Upload)]
    public IFormFile File { get; set; }
}
// Service layer snippet to store metadata
public async Task SaveFormAsync(IrbyFormsView model)
{
    var entity = new FormEntity
    {
        FormNumber = model.FormNumber,
        FileName   = model.FileName,
        FilePath   = model.FilePath,
        UploadedOn = DateTime.UtcNow
    };
    _dbContext.Forms.Add(entity);
    await _dbContext.SaveChangesAsync();
}

9. Conclusion

IrbyFormsView is a simple yet powerful ViewModel that:

  • Facilitates file uploads in ASP.NET Core MVC.
  • Encapsulates both metadata (FormNumber, FileName, FilePath) and the payload (IFormFile).
  • Can be extended with validation attributes and integrated into a full upload pipeline (Controller → Service → Storage → Database).

With this documentation, you now have:

  • A clear overview of its purpose.
  • Code samples for Controller and Razor views.
  • UML to visualize the class structure.
  • Best practices for validation and security.

🚀 Happy coding!