πŸ“„ IrbyImaging.Models.Upload.UploadViewModel.cs Documentation


πŸ“‘ Table of Contents

  1. 🎯 Overview & Purpose
  2. βš™οΈ Namespace & Dependencies
  3. 🏷️ Class: UploadViewModel
    1. πŸ”‘ Properties
    2. πŸ› οΈ Data Annotations
  4. πŸ”— Integration & Usage
    1. πŸ” Sample Controller & View
    2. πŸš€ Upload Workflow
  5. πŸ“ˆ UML Class Diagram
  6. βœ… Conclusion

🎯 Overview & Purpose

The UploadViewModel class is a ViewModel used within the IrbyImaging.UI application to facilitate CSV file uploads. It encapsulates all necessary data for:

  • Selecting an asset category
  • Uploading a CSV file via an HTML form

This lightweight model makes binding between your Razor (or MVC) view and controller seamless, while leveraging built‑in data validation.


βš™οΈ Namespace & Dependencies

using IrbyImaging.Utility;                // AssetCategory enum
using Microsoft.AspNetCore.Http;          // IFormFile for file upload
using System.ComponentModel.DataAnnotations; // Data validation attributes

namespace IrbyImaging.Models
{
    // ...
}
Dependency Purpose
IrbyImaging.Utility Defines the AssetCategory enum used by the model.
Microsoft.AspNetCore.Http Provides IFormFile interface for uploaded files.
System.ComponentModel.DataAnnotations Supplies [Required] and [Display] attributes for validation and display metadata.

🏷️ Class: UploadViewModel

/// <summary>
/// Class UploadViewModel.
/// </summary>
public class UploadViewModel
{
    /// <summary>
    /// Gets or sets the category.
    /// </summary>
    public AssetCategory Category { get; set; }

    /// <summary>
    /// Gets or sets the uploaded CSV.
    /// </summary>
    [Required]
    [Display(Name = "CSV File")]
    public IFormFile UploadedCsv { get; set; }
}

πŸ”‘ Properties

Property Type Attributes Description
Category AssetCategory β€” Asset category selected by the user (enum).
UploadedCsv IFormFile [Required][Display] CSV file uploaded by the user; must be provided.

πŸ› οΈ Data Annotations

  • [Required]
    Ensures that UploadedCsv is not null. If the user fails to provide a file, model validation will fail.

  • [Display(Name = "CSV File")]
    Overrides the label/name shown in views to β€œCSV File” rather than the property name UploadedCsv.


πŸ”— Integration & Usage

This ViewModel is designed to be used in a typical ASP.NET Core MVC or Razor Pages workflow.

πŸ” Sample Controller & View

1. Controller

using IrbyImaging.Models;
using Microsoft.AspNetCore.Mvc;

public class UploadController : Controller
{
    [HttpGet]
    public IActionResult Index()
    {
        // Initialize with a default category if needed
        var model = new UploadViewModel { Category = AssetCategory.Default };
        return View(model);
    }

    [HttpPost]
    public async Task<IActionResult> Index(UploadViewModel model)
    {
        if (!ModelState.IsValid)
        {
            // Return the form with validation errors
            return View(model);
        }

        // Process the uploaded CSV
        using var stream = model.UploadedCsv.OpenReadStream();
        // TODO: parse CSV, save data, etc.

        ViewBag.Message = "βœ… File uploaded successfully!";
        return View(model);
    }
}

2. Razor View (Index.cshtml)

@model IrbyImaging.Models.UploadViewModel

<form asp-action="Index" asp-controller="Upload" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label asp-for="Category"></label>
        <select asp-for="Category" asp-items="Html.GetEnumSelectList<AssetCategory>()"
                class="form-control"></select>
    </div>
    <div class="form-group">
        <label asp-for="UploadedCsv"></label>
        <input asp-for="UploadedCsv" type="file" class="form-control-file" />
        <span asp-validation-for="UploadedCsv" class="text-danger"></span>
    </div>
    <button type="submit" class="btn btn-primary">Upload πŸ“€</button>
</form>

@section Scripts {
    <partial name="_ValidationScriptsPartial" />
}

πŸš€ Upload Workflow

  1. Display Form
    • User selects an AssetCategory.
    • User picks a CSV file via file picker.
  2. Submit Form
    • Browser sends multipart/form-data to the UploadController.
  3. Model Binding & Validation
    • ASP.NET Core binds form data to UploadViewModel.
    • Validation attributes check for missing file.
  4. File Processing
    • On success, read model.UploadedCsv.OpenReadStream() to parse or store.
    • Return a success message or redirect as needed.

πŸ“ˆ UML Class Diagram

+---------------------------------+
|        UploadViewModel          |
+---------------------------------+
| - Category: AssetCategory       |
| - UploadedCsv: IFormFile        |
+---------------------------------+
| + (default ctor)                |
+---------------------------------+

AssetCategory
A simple enum defined in IrbyImaging.Utility (e.g., Default, Image, Video, etc.).


βœ… Conclusion

The UploadViewModel is a concise, easy‑to‑use representation for CSV file uploads in an ASP.NET Core MVC application. By combining the AssetCategory enum and the IFormFile interface, it:

  • Abstracts form data into a strongly-typed C# object
  • Enforces validation with data annotations
  • Integrates seamlessly with Razor views and controllers

Whether you’re importing asset metadata, bulk registering items, or processing custom CSV data, this ViewModel provides a solid foundation for your upload feature. πŸš€


Happy coding! πŸŽ‰