Null reference when putting a function in an @helper - where can I put it or how can I deal with it?

451 views Asked by At

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:

  1. an @helper function in an App_Code file - but this is the "null reference" error I encountered.
  2. a .cs class file (or maybe just add it to my ViewModel - but I cannot get the coding right.
  3. The suggestion to put it in the controller - but the code is lengthy.
  4. 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
}
1

There are 1 answers

2
DMulligan On BEST ANSWER

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.

  1. Fetch/create your business model(s), which contains all the information necessary to do this calculation.
  2. Perform the calculation on the business model. If your calculation is based on one object using only it's internal properties, you can include the calculation code within your business model itself. Otherwise I'd create a separate calculations class that computes it.
  3. Create your View model based on the calculation result and your business model. The view model should only contain the state that will be displayed/edited within the view.

For example:

public ActionResult YourAction(int userChoice)
{
    MyModel businessModel = createOrFetchModel(); //May be more than one object
    Calculations calcs = new Calculations(businessModel);
    var total = calcs.GetTotal(userChoice);
    MyViewModel viewModel = makeViewModel; //usually I'd use something like AutoMapper here
    return View(viewModel);
}

Edit : Sample code based on your update. The final design you must decide based on the actual calculation.

 public class Calculations
 {
     MyModelOne modelOne; //Do you really need seperate models for radio buttons?
     MyModelTwo modelTwo;
     Price Price;

     //...

     public decimal GetTotal()
     {
         decimal total = price.priceOne;
         total += price.FirstAddOn(modelOne);
         total += price.SecondAddOn(modelTwo);
         return total;
     }
 } 

 public class Price
 {
     //...
     public decimal FirstAddOn(MyModelOne modelOne)
     {
       if(modelOne.MyRadioButtonOne == MyModelOne.RB1Enum.RB1ChoiceOne)
          return this.AddOnOne;
       else if(modelOne.MyRadioButtonOne == MyModelOne.RB1Enum.RB1ChoiceTwo)
          return this.AddOnTwo;
       else
         return this.AddOnThree;
     }
 }