I am taking a prior question one step further (see this question), I am trying to figure out how to sum
two (or more) selections the user makes with, for example, a radio button list. The selection the user makes is tied to an entity that contains a static currency value using if/else if
statements.
These are the entities for price:
[DataType(DataType.Currency)]
[DisplayFormat(DataFormatString = "{0:c}")]
public decimal priceProcessingStandard = 0;
[DataType(DataType.Currency)]
[DisplayFormat(DataFormatString = "{0:c}")]
public decimal priceProcessingExpedited = 250;
[DataType(DataType.Currency)]
[DisplayFormat(DataFormatString = "{0:c}")]
public decimal priceSubmissionOnline = 0;
[DataType(DataType.Currency)]
[DisplayFormat(DataFormatString = "{0:c}")]
public decimal priceSubmissionManual = 200;
So, if I have two sets of if/else if
statements such as:
@if (Model.ProcessingRadioButtons == Processing.Standard)
{
@Html.DisplayFor(m => m.priceProcessingStandard)
}
else if (Model.ProcessingRadioButtons == Processing.Expedited)
{
@Html.DisplayFor(m => m.priceProcessingExpedited)
}
...
@if (Model.SubmissionRadioButtons == Submission.Online)
{
@Html.DisplayFor(m => m.priceSubmissionOnline)
}
else if (Model.SubmissionRadioButtons == Submission.Manual)
{
@Html.DisplayFor(m => m.priceSubmissionManual)
}
and the user makes selections in the two separate radio button lists corresponding to Processing.Expedited
and Submission.Manual
, the code will respectively display $250.00
and $200.00
.
I cannot, however, figure out how to sum
those two to display $450.00
. Bear in mind, I do not know the selections before hand, so doing priceProcessingExpedited + priceSubmissionManual
in a function and then calling it will obviously not work. Also, I am doing about 10-15 of these but I only used two simple ones as an example of what I am trying to accomplish (so the fact that the other two choices are $0.00
doesn't mean anything because there are varying prices for other choices that I left out).
Any guidance?
UPDATE: Based on suggestion in answer, I am doing this:
Model.calculated =
Model.priceSolution +
((Model.ProcessingRadioButtons == Processing.Standard) ?
Model.priceProcessingStandard :
(Model.ProcessingRadioButtons == Processing.Expedited) ?
Model.priceProcessingExpedited :
Model.priceProcessingUrgent);
Some notes:
priceSolution
is a static value that I use as a base (it's the base value plus the user selections).- I am using
calculated
in the ViewModel andget; set;
'ing it. - I left out the
Namespace.ViewModels.MyData
beforeProcessing.
for brevity. - I left out
Submission
for brevity as it's just a+
then the same logic as inProcessing
.