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 7695083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:28:01+00:00 2026-05-31T21:28:01+00:00

I am trying to conditionally calculate some values based on user inputs in a

  • 0

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?

  • 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-05-31T21:28:03+00:00Added an answer on May 31, 2026 at 9:28 pm

    I figured out how to do this (might not be clean/efficient/standard practice, but it got the job done):

     Model.calculated =
            Model.PriceQuote.priceChapter7Solution +
            ((Model.radiobuttonlist1 == 
                Namespace.ViewModels.MyModel.radio1.radio1selection1) ?
            Model.option1 :
            (Model.radiobuttonlist1 == 
                Namespace.ViewModels.MyModel.radio1.radio1selection2) ?
            Model.option2 :
            Model.nooption);
    

    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 as a ? b : (c ? d : e). See Docs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to conditionally update my updatepanel based on changes that have been
I'm trying to to add a post-build macro that would conditionally copy some files
I'm trying to create a basic database trigger that conditionally deletes rows from database1.table1
I am trying to set some public static constants on a class conditionally by
I'm trying to calculate the conditional median of a chart that looks like this:
I am trying to conditionally format the numbers that appear in a NumericAxis axis
I'm trying to conditionally hide or show a <p:dataGrid> based on a session bean
Ok, let's say I'm trying to validate an object conditionally based upon the value
I am trying to calculate the correct mouse coordinates, in user coordinate space, in
I have made a php form that is submitted by email. I am trying

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.