Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7977681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:16:23+00:00 2026-06-04T09:16:23+00:00

I am trying to calculate a price, something like: @{ Model.Prices.total = ((Input ==

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T09:16:25+00:00Added an answer on June 4, 2026 at 9:16 am

    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;
         }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can you please help me with this? I am trying to calculate the total
I am using three tables and I am trying to calculate discounted price by
I'm trying to calculate age in flex. I've found this previous question What is
I'm trying to calculate TimeSpans between dates. I have no problem with this if
I am trying to calculate sum of 'price' field of an 'observableArray'. I have
Im trying to calculate a subtotal and a total from a series of values
I'm trying to calculate the average net price of a product. I have, in
I'm trying to write a function that calculates the grand total price of each
I'm trying to dynamically calculate the total of an order using Django/Python and Javascript.
I'm trying to generate a price based on radio button selections, on input fields

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.