π IrbyImaging.Models.Upload.UploadViewModel.cs Documentation
π Table of Contents
- π― Overview & Purpose
- βοΈ Namespace & Dependencies
- π·οΈ Class:
UploadViewModel - π Integration & Usage
- π UML Class Diagram
- β 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 thatUploadedCsvis 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 nameUploadedCsv.
π 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
- Display Form
- User selects an
AssetCategory. - User picks a CSV file via file picker.
- User selects an
- Submit Form
- Browser sends
multipart/form-datato theUploadController.
- Browser sends
- Model Binding & Validation
- ASP.NET Core binds form data to
UploadViewModel. - Validation attributes check for missing file.
- ASP.NET Core binds form data to
- File Processing
- On success, read
model.UploadedCsv.OpenReadStream()to parse or store. - Return a success message or redirect as needed.
- On success, read
π UML Class Diagram
+---------------------------------+
| UploadViewModel |
+---------------------------------+
| - Category: AssetCategory |
| - UploadedCsv: IFormFile |
+---------------------------------+
| + (default ctor) |
+---------------------------------+
AssetCategory
A simpleenumdefined inIrbyImaging.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! π