📑 Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. Class: RequestOptionForUpdateViewModel
  5. UML Class Diagram
  6. Usage Examples
  7. Conclusion

🌟 Overview & Purpose

The RequestOptionForUpdateViewModel is part of the IrbyImaging.UI project. It transforms an existing RequestOptionViewModel into a model fitted for rendering and updating options in the UI.

  • It encapsulates both the raw data (index, text, approval details) and an HTML snippet (Input) to display a “Mark Complete” toggle.
  • Designed for MVC/Razor views, it helps build forms where each request option can be marked complete and shows approval state & messages.

🔧 Dependencies & Technologies

Category Description
Platform .NET (C#)
Namespace IrbyImaging.ViewModels
Related ViewModel RequestOptionViewModel (input model)
Front‑End Library Bootstrap CSS classes (btn, btn-xs)
Browser Behavior Uses data-toggle="buttons" for toggle UI

🚀 Key Functional Areas

  1. Data Mapping
    • Converts a RequestOptionViewModel into an updatable model.
  2. HTML Generation
    • Produces a small HTML fragment for a “Mark Complete” button group.
  3. Approval Workflow
    • Carries approval metadata (required, department, decision, message).

🏗️ Class: RequestOptionForUpdateViewModel

Properties

Property Type Description
Index int Position or identifier of the option.
Item string The display text of the request option.
Input string HTML snippet for “Mark Complete” control (empty if already completed).
ApprovalRequired bool Indicates if approval is needed.
ApprovalDepartment string Department responsible for approval.
HasApprovalDecision bool Whether a decision has been made.
IsApproved bool Indicates final approval status.
ApprovalMessage string Optional message from the approver.

Constructor

/// <summary>
/// Initializes a new instance of the RequestOptionForUpdateViewModel 
/// by mapping from a RequestOptionViewModel.
/// </summary>
/// <param name="request">The source view model.</param>
public RequestOptionForUpdateViewModel(RequestOptionViewModel request)
{
    Index               = request.Index;
    Item                = request.Item;
    Input               = GenerateHtml(Index, Item, request.IsCompleted);
    ApprovalRequired    = request.ApprovalRequired;
    ApprovalDepartment  = request.ApprovalDepartment;
    HasApprovalDecision = request.HasApprovalDecision;
    IsApproved          = request.IsApproved;
    ApprovalMessage     = request.ApprovalMessage;
}
  • Purpose: Copies data from the source model and invokes GenerateHtml to build the Input property.

Methods

GenerateHtml(int index, string item, bool isCompleted)

/// <summary>
/// Builds the HTML string for the “Mark Complete” toggle button.
/// Returns an empty string if the option is already completed.
/// </summary>
/// <param name="index">Unique index for DOM IDs</param>
/// <param name="item">Label text (unused directly in HTML)</param>
/// <param name="isCompleted">If true, skip rendering control</param>
/// <returns>HTML string</returns>
private string GenerateHtml(int index, string item, bool isCompleted)
{
    // If already completed, no toggle is needed.
    if (isCompleted) 
        return string.Empty;

    // Otherwise render a Bootstrap-styled toggle button.
    return "<div class='btn-group' data-toggle='buttons' style='margin-bottom: 4px'>" +
           $"<label class=\"btn btn-warning btn-xs\" id=\"label{index}\">" +
           $"<input type=\"checkbox\" autocomplete=\"off\" id=\"checkbox{index}\">" +
           $"<span id='span{index}'>Mark Complete</span>" +
           "</label></div>";
}
  • Logic
    • Checks isCompleted; if true, suppresses the control (read‑only state).
    • Otherwise, uses Bootstrap classes to create a compact checkbox toggle.

📐 UML Class Diagram

+----------------------------------------------------------+
|                RequestOptionForUpdateViewModel           |
+----------------------------------------------------------+
| - Index: int                                             |
| - Item: string                                           |
| - Input: string                                          |
| - ApprovalRequired: bool                                 |
| - ApprovalDepartment: string                             |
| - HasApprovalDecision: bool                              |
| - IsApproved: bool                                       |
| - ApprovalMessage: string                                |
+----------------------------------------------------------+
| + RequestOptionForUpdateViewModel(request: RequestOptionViewModel) |
| - GenerateHtml(index: int, item: string, isCompleted: bool): string|
+----------------------------------------------------------+

🔧 Usage Examples

1. Constructing the ViewModel

// Suppose you have the original RequestOptionViewModel list
List<RequestOptionForUpdateViewModel> updateModels = originalList
    .Select(r => new RequestOptionForUpdateViewModel(r))
    .ToList();

2. Rendering in a Razor View

@model IEnumerable<RequestOptionForUpdateViewModel>

<table class="table">
  <thead>
    <tr><th>#</th><th>Option</th><th>Action</th><th>Approval</th></tr>
  </thead>
  <tbody>
  @foreach(var opt in Model)
  {
    <tr>
      <td>@opt.Index</td>
      <td>@opt.Item</td>
      <td>
        @Html.Raw(opt.Input)  <!-- Renders the Mark Complete toggle -->
      </td>
      <td>
        @if(opt.HasApprovalDecision) {
          <span class="badge @(opt.IsApproved ? "badge-success" : "badge-danger")">
            @opt.ApprovalMessage
          </span>
        } else {
          <em>Pending (@opt.ApprovalDepartment)</em>
        }
      </td>
    </tr>
  }
  </tbody>
</table>

3. Handling Post-Back

[HttpPost]
public IActionResult UpdateOptions(List<RequestOptionForUpdateViewModel> model)
{
    // Iterate and check checkboxes via form values (e.g., Request.Form)
    // Apply business logic, save updates, send for approval, etc.
    return RedirectToAction("Index");
}

⚠️ Error Handling & Edge Cases

  • Completed Items: No toggle rendered → UI cannot re‑toggle a completed option.
  • Unique IDs: index must be unique per page render to avoid duplicate element IDs.
  • Null Input: Assumes RequestOptionViewModel is non-null and has non-null Item.

✅ Conclusion

The RequestOptionForUpdateViewModel is a focused ViewModel that:

  • Maps raw request option data.
  • Augments it with an HTML control snippet for user interaction.
  • Carries approval workflow metadata.

This design cleanly separates concerns:

  • Data Transfer (ViewModel)
  • Presentation Logic (HTML generation)

Use it in your MVC/Razor pages to build intuitive, toggle‑based request update forms! 🚀