π RequestOptionForCreateViewModel Documentation
File:
IrbyImaging/ViewModels/RequestOptionForCreateViewModel.cs
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Class: RequestOptionForCreateViewModel
- UML Diagram
- Usage Examples
- Sample Razor View Integration
- 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: DomainRequestOptionobject (contains raw data).index: Unique index for ID generation.
-
Behavior:
- Maps
request.ItemβItem - Maps
request.CostβCost - Sets
Index=index - Sets
ApprovalRequired&ApprovalDepartment - Calls
GenerateHtml(...)to buildInput
- Maps
π οΈ Private Methods
GenerateHtml
private string GenerateHtml(int index, string item, RequestOptionType type)
-
Purpose: Build two HTML input elements per option:
- Checkbox for user selection
<input type="checkbox" id="checkbox{index}" name="{item}" /> - Hidden field to transport the option type
<input type="hidden" id="hidden{index}" name="{type}" />
- Checkbox for user selection
-
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:
RequestOptionForCreateViewModelencapsulates presentation logic (HTML generation) away from Controllers and Views. - Ease of Use: Ready-to-render
Inputproperty 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! π