While working on generating HTML to be passed back from a REST method, I encountered the following code snippet:
StringBuilder builder = new StringBuilder();
builder.Append("<div class=\"row\">");
builder.Append("<div class=\"col-md-6\">");
builder.Append("<div class=\"bottomleft\">");
// TODO: Replace the hardcoded val ("$9,814.81") with the actual val
builder.Append("<h2 class=\"sectiontext\">Forecasted Spend - $9,814.81</h2>");
(initial HTML structure has been set up before this). Subsequently, live data is added as shown below:
builder.Append("<table>");
builder.Append("<tr>");
builder.Append("<th>Item Code</th>");
builder.Append("<th class=\"rightjustifytext\">Last Week's Usage</th>");
builder.Append("<th class=\"rightjustifytext\">This Week's Price</th>");
builder.Append("<th class=\"rightjustifytext\">Forecasted Spend</th>");
builder.Append("</tr>");
decimal forecastedSpend;
int lastWeeksUsage;
int loopCount = 0;
int totalUsage = 0;
decimal totalForecastedSpend = 0.0M;
foreach (DataRow dr in dtViewPriceMatrixVarianceResults.Rows)
{
// Data rows are dynamically generated here
}
(The totals are calculated and added at the end) The final step involves closing the elements and returning the prepared HTML content within the StringBuilder:
builder.Append("</table>");
builder.Append("</div>");
builder.Append("</div>");
return builder.ToString();
The challenge arises when trying to replace the placeholder value "$9,814.81" with the actual figure of totalForecastedSpend, which is determined during the population of the table rows with live data. While a workaround involving storing values in a generic list and utilizing them later works, I am exploring alternative solutions using HTML or CSS. Is there a way to achieve this? Or would adopting such an approach be detrimental?
The related REST method is defined as follows:
[Route("{unit}/{begdate}/{enddate}")]
public HttpResponseMessage GetQuadrantData(string unit, string begdate, string enddate)
{
// Implementation logic for assembling HTML content
}
Can the issue of replacing the placeholder value with the actual totalForecastedSpend be addressed through HTML/CSS manipulation, or should another strategy be pursued?
UPDATE
Although the initial question remains unresolved, implementing a solution involving pre-calculating values and storing them in a list proves to be effective:
A dedicated class is created:
public class ForecastedSpend
{
public String ItemCode { get; set; }
public int LastWeeksUsage { get; set; }
public Decimal Price { get; set; }
public Decimal ForecastedSpendCalcd { get; set; }
}
The earlier code segment is replaced with:
// Pre-calculation and storage logic using ForecastedSpend class
// Table row generation based on stored values
This revised approach successfully handles the scenario.