I've seen what seem to be similar questions on here but they all seemed very contextual and I've so far had some trouble figuring this out. Still pretty new to C# and razor templating so apologies if this is a repeat question or ridiculously simple!
What I'm trying to do is take the value of a user defined string inside of a foreach loop, convert it to (I assume) an int, add it up for each instance of the loop, and then check the sum of the property.
Here is a dumbed down code example that might make this clearer:
foreach (var col in columnOptions.Columns)
{
<div class="@col.Width">
@*content*@
</div>
}
if (totalWidthValue > 12)
{
@*do something else*@
}
So basically I need to take the value of col.Width, convert it to an int so it can be calculated, add up the total of each occurrence, and then check whether the total is greater than 12.
Also, in case it helps, this is where/how width is getting set:
[JsonSchema(Title = "Column width", Description = "Select the width of this column based on a 12 column wide grid")]
[PropertyOrder(11)]
[JsonOptions(GridColumns = 6)]
[JsonEnumValues("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12")]
[DefaultValue("12")]
[Localizable(false)]
public string Width { get; set; }
EDIT: Here is what I can provide in terms of my attempts, I fear it may not be fully formed enough to pinpoint any specific "here's where you went wrong", but at least it shows an attempt and maybe gives some context to where my knowledge gaps are (the added parts really are just the two variables):
foreach (var col in columnOptions.Columns)
{
var colNum = double.TryParse(col.Width, out colNum);
var totalWidthValue = colNum + colNum;
<div class="@col.Width">
@*content*@
</div>
}
if (totalWidthValue > 12)
{
@*do something else*@
}