I am trying to calculate a price, something like:
@{
Model.Prices.total =
((Input == choice) ? a : b)
+
((AnotherInput == anotherchoice) ? c : d)
;
}
This works well inside the .cshtml
view file, but needless to say the actual code is a LOT longer (not to mention that I want to apply 3 such different calculations).
So I got to thinking of making an @helper
file in App_Code
called Calculate
in Pricing.cshtml
so that I could call it like this:
@Pricing.Calculate()
But this will throw a "cannot perform runtime binding on a null reference" error.
I get the error, and I get that's a limitation. Can anyone suggest how I can go about accomplishing this? I thought a about a class file but wouldn't know how to convert it into one (if that's a better alternative). A code sample is very much appreciated.
UPDATE: I was placing this calculation temporarily in the view because I am unable to convert it to other options I've researched:
- an
@helper
function in anApp_Code
file - but this is the "null reference" error I encountered. - a
.cs
class file (or maybe just add it to my ViewModel - but I cannot get the coding right. - The suggestion to put it in the controller - but the code is lengthy.
Creating a custom Html Helper class to try and call it like `@Html.Calculate([parameter?], [parameter?]) - but for me that's a bit more complicated than #2.- comment duly noted.
Again, note that the example I provided is a bare-bones, simple one. I've got 30+ conditions to explore each with multiple options (the a
's and b
's above, for example), and some of them get more complex:
() ? a : () ? b : c + () ? d : () ? e : () ? f : g + ....
And if I go C# code (either in it's own .cs
file, a custom Html Helper or in the ViewModel) it's all the declarations that I'm going to get wrong.
Mind you, it's not the actual calculation I've got a problem with. That snippet immediately above works to perform my calculations. I am just struggling to get it to an appropriate option I listed above.
UPDATE 2
I am having a lot of trouble converting this to it's own class (I am not a programmer - sorry for not understanding). This is part of my function in the view (just a snippet, hopefully I can figure out the rest if you provide a code sample):
@{
Model.Price.calculated
=
//below is the "base" price, all else would be "add-ons"
Model.Price.priceOne //elsewhere would be priceTwo, etc.
+
((Model.MyModelOne.MyRadioButtonOne ==
MyModelOne.RB1Enum.RB1ChoiceOne)
?
Model.Price.AddOnOne
:
(Model.MyModelOne.MyRadioButtonOne ==
MyModelOne.RB1Enum.RB1ChoiceTwo)
?
Model.Price.AddOnTwo
:
Model.Price.AddOnThree)
+
((Model.MyModelTwo.MyRadioButtonTwo
== MyModelTwo.RB2Enum.RB2ChoiceOne)
?
Model.Price.AddOnFour
:
(Model.MyModelTwo.MyRadioButtonTwo
== MyModelTwo.RB2Enum.RB2ChoiceTwo)
?
Model.Price.AddOnFive
:
Model.Price.AddOnSix)
;
}
Don't bust my balls for using enums
. :)
Again, although the calculation is not the appropriate thing to do in a view, the above works - I get the calculated result I am expecting based on user choices.
My Price.cs
model (again, just something basic so you get the idea):
public class Price
{
//Leaving out [DataType] and [DisplayFormat] DataAnnotations
// leaving out priceTwo, etc.
// numbers are basic for simplicity
public decimal calculated { get; set; }
public decimal priceOne { get { return 100; } }
public decimal AddOnTwo { get { return 10; } }
public decimal AddOnThree { get { return 20; } }
public decimal AddOnFour { get { return 30; } }
public decimal AddOnFive { get { return 40; } }
public decimal AddOnSix { get { return 50; } }
// Others go here
}
Html helper functions are just ways to reuse duplicated pieces of your display. What you're doing now is calculations based on the state of your model. In MVC, this data processing would be considered part of the model.
Here's the basic steps I'd take.
For example:
Edit : Sample code based on your update. The final design you must decide based on the actual calculation.