@foreach (var destination in Split.Destinations) { } @if (Split.Destinations.Count > 1) { }
Destination Percentage Actions
%
Sending @(100 - Leftover)% to @Split.Destinations.Count destinations
@code { [CascadingParameter] private EditContext CascadedEditContext { get; set; } public decimal Leftover => 100 - Split.Destinations.Sum(split => split.Percentage); [Parameter] public Split Split { get; set; } [Parameter] public EventCallback OnRequestRemove { get; set; } private void CreateDestination() { Split.Destinations.Add(new Prism.PrismSplit()); } private void RemoveSplit() { OnRequestRemove.InvokeAsync(Split); } private void UpdateDestinationValue(Prism.PrismSplit destination, object eValue) { var newValue = Math.Min(100, Math.Round(Convert.ToDecimal(eValue), 2)); var allowedMax = Math.Round(destination.Percentage + Leftover, 2); if (newValue > allowedMax) { //take the difference from the other destinations proportionally var difference = Math.Round(newValue - allowedMax, 2); var otherDestinations = Split.Destinations.Where(split => split != destination).ToList(); var totalPercentage = Math.Round(otherDestinations.Sum(split => split.Percentage), 2); foreach (var otherDestination in otherDestinations) { var percentage = otherDestination.Percentage; var newPercentage = Math.Round(percentage - (percentage / totalPercentage * difference), 2); otherDestination.Percentage = newPercentage; } } destination.Percentage = newValue; } }