I am trying to conditionally calculate some values based on user inputs in a form. The “inputs” themselves tie into the calculation, in that if a user is presented with two radio button lists their selections will determine whether or not a currency amount is attached, and then later added together. I don’t understand jquery, and so I cannot figure out how to use something like knockout.js to do what I want (besides, my calculation will be served up in a confirmation view only and will not be “re-calculated” unless the user goes back and changes their selections).
Note, I am not talking about the scenario where you have a textbox that accepts a value and adding it to another textbox value the user inputs (e.g., user enters 10, then 10, then form calculates 20). The values are static based upon selection.
For context, I am using Serializer and a wizard which is stepping through and passing model selections/inputs to the subsequent view.
I can do something like this as an example (it’s simple, but based on responses I can figure out how to build it up to the more complex scenario I have):
Model.cs:
[DataType(DataType.Currency)]
[DisplayFormat(DataFormatString = "{0:c}")]
public decimal calculated { get; set; }
[DataType(DataType.Currency)]
[DisplayFormat(DataFormatString = "{0:c}")]
public decimal option1 = 500;
[DataType(DataType.Currency)]
[DisplayFormat(DataFormatString = "{0:c}")]
public decimal option2 = 100;
//Following is just my idea to calculate $0 if
//a selection results in a "no value option"
//although I suppose if nothing is selected I
//can just avoid calculating + $0
[DataType(DataType.Currency)]
[DisplayFormat(DataFormatString = "{0:c}")]
public decimal nooption = 0;
Then in Confirm.cshtml:
@{
Model.calculated = Model.option1 + Model.option2 + Model.nooption;
}
....
@Html.DisplayFor(m => m.calculated)
will display $600.00. That’s easy enough.
However, my goal is threefold. First, I need to tie in, for example, option1 with the selection from a radiobutton list, and option2 with another radiobutton list depending on what the user selects. Second, depending on the selection I want to conditionally calculate all the options present which will obviously vary based on user selection (some users will select option1 and option2, others just option1, others just option2, still others neither). Third, where is the best place to have the calculation take place (the view, a .cs file, the controller (although I am trying to minimize code there), etc.).
So, if I have these enums for the radiobutton lists (each option corresponding to an option for now, later I will discuss two options per radiobutton list enum):
public enum radio1
{
radio1selection1, // this would correspond to nooption (or $0)
radio1selection2 // this would correspond to option1 (or $500)
}
public enum radio2
{
radio2selection1, // this would correspond to nooption (or $0)
radio2selection2 // this would correspond to option2 (or $100)
}
I’ve indicated in the enums how I’d like to “tie” the option to the selection.
My problem is in trying to do if statements, which I cannot figure out properly. Something like this in Confirm.cshtml:
@{
if (Model.radio1 == radio1selection1)
Model.calculated = Model.nooption;
else....
}
The above is completely wrong, I know. I am trying to think of all the permutations of calculating, but I don’t think its worth the effort because it would either be wrong or it would work but be too long when there is an easier way. I was even thinking of trying case/break but that too seems oddly the wrong approach.
What’s adding to the complexity for me is if my enums have more than two selections, and those other selections result in several options, say option1 and option1a, and depending on which the user selects will determine the calculation. So the enum becomes:
public enum radio1
{
radio1selection1, // this would correspond to nooption (or $0)
radio1selection2, // this would correspond to option1 (or $500)
radio1selection3 // this would correspond to NEW
// option1a (or $750) (with a corresponding
// "int" named "option1a")
}
One of the things I thought of doing was to just display all the options selected in a <table> in the view, so that even if something is a $0 value, it would just say $0 as the option. Then I thought I could just add all the values present. But still I don’t know the best way to accomplish that.
Any opinions on how I would be able to and or all three of my stated goals above?
I figured out how to do this (might not be clean/efficient/standard practice, but it got the job done):
I could just add
+after... : Model.nooption)instead of ending the statement with;to calculate conditions on other options.Originally I did not know that the conditional (
? :) operator could contain more than two expressions – i.e., it’s right associative so additional conditions will be evaluated asa ? b : (c ? d : e). See Docs.