πŸ“„ RequestOptionForCreateViewModel Documentation

File: IrbyImaging/ViewModels/RequestOptionForCreateViewModel.cs


πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Class: RequestOptionForCreateViewModel
  4. UML Diagram
  5. Usage Examples
  6. Sample Razor View Integration
  7. Conclusion

πŸ” Overview & Purpose

The RequestOptionForCreateViewModel is a ViewModel class designed for the Create page of the Request Options feature in the IrbyImaging application. It wraps domain data into a format that's readily consumable by a Razor view, providing:

  • A sequential Index for identification.
  • Human-readable Item & Cost descriptions.
  • A pre-built HTML string (Input) to render checkboxes and hidden fields.
  • Approval metadata to indicate if this option requires departmental approval.

Why?
In MVC, ViewModels separate UI concerns from Domain logic. This class ensures the Create view only deals with ready-to-render properties without knowing business internals.


🧰 Dependencies & Technologies

Package/Namespace Purpose
IrbyImaging.DomainClasses Contains the RequestOption domain object
IrbyImaging.Utility Defines the RequestOptionType enum used to distinguish types
ASP.NET MVC Hosting environment (ViewModels pattern, Razor integration)

πŸ—οΈ Class: RequestOptionForCreateViewModel

namespace IrbyImaging.ViewModels
{
    public class RequestOptionForCreateViewModel
    {
        // ... properties, constructor, methods ...
    }
}

πŸ”‘ Properties

Name Type Description
Index int Zero-based index used for generating unique HTML element IDs
Item string Display text for the request option
Cost string Cost representation (e.g., "$100.00")
Input string Pre-built HTML snippet (checkbox + hidden field)
ApprovalRequired bool Indicates if selecting this option triggers an approval workflow
ApprovalDepartment string Department to route the approval request to

πŸš€ Constructor

public RequestOptionForCreateViewModel(RequestOption request, int index)
  • Parameters:

    • request : Domain RequestOption object (contains raw data).
    • index : Unique index for ID generation.
  • Behavior:

    1. Maps request.Item β†’ Item
    2. Maps request.Cost β†’ Cost
    3. Sets Index = index
    4. Sets ApprovalRequired & ApprovalDepartment
    5. Calls GenerateHtml(...) to build Input

πŸ› οΈ Private Methods

GenerateHtml

private string GenerateHtml(int index, string item, RequestOptionType type)
  • Purpose: Build two HTML input elements per option:

    1. Checkbox for user selection
      <input type="checkbox" id="checkbox{index}" name="{item}" />
    2. Hidden field to transport the option type
      <input type="hidden" id="hidden{index}" name="{type}" />
  • Returns: Combined HTML string:

    <input type="checkbox" id="checkbox0" name="Extra Copies" />
    <input type="hidden"   id="hidden0"   name="PrintOption" />
    

πŸ“Š UML Diagram

classDiagram
    class RequestOptionForCreateViewModel {
        +int Index
        +string Item
        +string Cost
        +string Input
        +bool ApprovalRequired
        +string ApprovalDepartment
        +RequestOptionForCreateViewModel(RequestOption, int)
        -string GenerateHtml(int, string, RequestOptionType)
    }

    class RequestOption {
        <<DomainClass>>
        +string Item
        +string Cost
        +RequestOptionType Type
        +bool ApprovalRequired
        +string ApprovalDepartment
    }

    RequestOption --o RequestOptionForCreateViewModel : uses

πŸ”§ Usage Examples

1. Preparing ViewModels from Domain Data

using IrbyImaging.DomainClasses;
using IrbyImaging.ViewModels;

// Suppose you fetch these from your service/repository:
IEnumerable<RequestOption> options = requestOptionService.GetAll();

// Transform to ViewModel list:
var viewModels = options
    .Select((opt, idx) => new RequestOptionForCreateViewModel(opt, idx))
    .ToList();

2. Passing to ASP.NET MVC Controller

public IActionResult Create()
{
    var domainOptions = _optionService.GetAll();
    var viewModels = domainOptions
        .Select((o, i) => new RequestOptionForCreateViewModel(o, i))
        .ToList();

    return View(viewModels);   // Model: List<RequestOptionForCreateViewModel>
}

🎨 Sample Razor View Integration

@model IEnumerable<IrbyImaging.ViewModels.RequestOptionForCreateViewModel>

<h2>Create New Request</h2>
<form asp-action="Create" method="post">
    <table>
        <thead>
            <tr>
                <th>Select</th>
                <th>Option</th>
                <th>Cost</th>
                <th>Approval</th>
            </tr>
        </thead>
        <tbody>
        @foreach (var vm in Model) {
            <tr>
                <td>@Html.Raw(vm.Input)</td>
                <td>@vm.Item</td>
                <td>@vm.Cost</td>
                <td>
                    @if (vm.ApprovalRequired) {
                        <span>Required by @vm.ApprovalDepartment</span>
                    } else {
                        <span>None βœ…</span>
                    }
                </td>
            </tr>
        }
        </tbody>
    </table>
    <button type="submit">Submit</button>
</form>

βœ… Conclusion

  • Separation of Concerns: RequestOptionForCreateViewModel encapsulates presentation logic (HTML generation) away from Controllers and Views.
  • Ease of Use: Ready-to-render Input property simplifies Razor markup.
  • Extensible: You can extend properties or HTML generation to include CSS classes, data-attributes, or client‑side scripts.

With this ViewModel in place, your Create page remains clean, maintainable, and decoupled from domain representation! πŸš€