๐Ÿ“š IrbyImaging.Services.Helpers Documentation


๐Ÿš€ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Class Diagram (UML)
  4. Helper Methods / Utilities
  5. Usage Examples
  6. Error Handling & Return Types
  7. Conclusion

๐Ÿ“– 1. Overview & Purpose

The Helpers class in the IrbyImaging.Services namespace provides utility methods to streamline the creation of ASP.NET Core MVC SelectList objects. These lists are commonly used to populate <select> dropdowns in Razor views.

Key Goals

  • ๐ŸŽฏ Standardize the way dropdown data is shaped (expects dataValue and dataText fields).
  • ๐ŸŽฏ Simplify controller and view code by encapsulating boilerplate SelectList construction.
  • ๐ŸŽฏ Support default selection via an overloaded method.

๐Ÿ›  2. Dependencies & Technologies

Technology / Package Version Purpose
Microsoft.AspNetCore.Mvc.Rendering (Core) Provides the SelectList type
.NET Standard / .NET Core 2.x โ€“ 6.x Base framework for ASP.NET Core applications
C# 8.0+ Language
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Collections.Generic;

๐Ÿ“ 3. Class Diagram (UML)

+-------------------------+
| <<static>> Helpers      |
+-------------------------+
| +GenerateSelectListFor( |
|     IEnumerable<object> |
|     items) : SelectList |
|                         |
| +GenerateSelectListFor( |
|     IEnumerable<object> |
|     items,             |
|     object selectedValue) : SelectList |
+-------------------------+

๐Ÿ”ง 4. Helper Methods / Utilities

The Helpers class exposes two static methodsโ€”both named GenerateSelectListFor, differing in parameters. Their job is to wrap the ASP.NET Core SelectList constructor and fix the data field names to dataValue and dataText.

1. GenerateSelectListFor(IEnumerable items)

public static SelectList GenerateSelectListFor(IEnumerable<object> items)
Parameter Type Description
items IEnumerable<object> A collection of objects, each having dataValue & dataText properties.

Returns:
SelectList โ€” binds to items, maps dataValue as the value and dataText as the display text.

Usage Scenario:
When you only need to populate a dropdown without pre-selecting any item.


2. GenerateSelectListFor(IEnumerable items, object selectedValue)

public static SelectList GenerateSelectListFor(IEnumerable<object> items, object selectedValue)
Parameter Type Description
items IEnumerable<object> Collection of items with dataValue & dataText properties.
selectedValue object The value that should be selected by default in the dropdown.

Returns:
SelectList โ€” same as above, but with one item marked as selected.

Usage Scenario:
When you need to show a previously chosen value (e.g., editing a form).


๐Ÿ’ก 5. Usage Examples

// 1. Define a list of anonymous objects:
var countryItems = new List<object> {
    new { dataValue = "US", dataText = "United States" },
    new { dataValue = "CA", dataText = "Canada" },
    new { dataValue = "MX", dataText = "Mexico" }
};

// 2. Generate an unselected SelectList:
var countrySelectList = Helpers.GenerateSelectListFor(countryItems);

// 3. Generate a SelectList with default selected value "CA":
var countrySelectListWithDefault = Helpers.GenerateSelectListFor(countryItems, "CA");

// 4. In your Razor view (e.g., .cshtml):
@Html.DropDownListFor(
    model => model.SelectedCountry, 
    countrySelectListWithDefault, 
    "-- Select Country --", 
    new { @class = "form-control" }
)

๐Ÿšจ 6. Error Handling & Return Types

  • Return Type: Both methods always return a non-null SelectList.
  • Exceptions:
    • If items is null, new SelectList(null, ...) will still create an empty list; no exception is thrown.
    • If objects in items do not contain dataValue/dataText properties, runtime errors occur when binding.

Best Practice:
Validate or shape your data models to include dataValue & dataText before passing to these helpers.


๐Ÿ 7. Conclusion

The Helpers class is a lightweight utility for ASP.NET Core MVC apps in the IrbyImaging solution. By enforcing a consistent naming scheme (dataValue / dataText) and abstracting SelectList construction, it:

  • ๐Ÿ”„ Reduces repeated code in controllers.
  • ๐Ÿงฉ Improves maintainabilityโ€”all dropdown logic lives in one place.
  • ๐Ÿ‘ Enhances readabilityโ€”calling Helpers.GenerateSelectListFor(...) is selfโ€‘documenting.

Happy coding! โœจ