How can I SUM two different selections passed to m

2019-09-09 10:18发布

问题:

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:

  1. priceSolution is a static value that I use as a base (it's the base value plus the user selections).
  2. I am using calculated in the ViewModel and get; set;'ing it.
  3. I left out the Namespace.ViewModels.MyData before Processing. for brevity.
  4. I left out Submission for brevity as it's just a + then the same logic as in Processing.

回答1:

You do know the selections before hand considering your @if (Model.SubmissionRadioButtions == Submission.Online) is a test against values currently held by the model - even if this is only after a POST.

As such, you should create a property in your view model that also performs these tests and sums the appropriate fields.

If you don't want this property displayed before the POST, make the property return a nullable type and wrap the view with @if(MySum.HasValue) { @Html.DisplayFor(m=>m.MySum) }