开发者

Best way to integrate values into enum (dictionary), then calculating based on user selection in form

Any guidance or pointing me to an example would be much appreciated (I can't formulate a good search term on the Googleplex).

I have a model using enums that i define in a dictionary and then render on the view with @Html.RadioButtonFor, etc.

Here is an example of my model:

public PaymentPlanList PaymentPlan { get; set; }
        public enum PaymentPlanList
        {
            PaymentPlan_One,
            PaymentPlan_Two,
        }
        public class PaymentPlanDictionary
        {
            public static readonly Dictionary<PaymentPlanList, string> paymentplanDictionary = new Dictionary<PaymentPlanList, string>
            {
            { PaymentPlanList.PaymentPlan_One, "One full payment in advance (receive the lowest price)." },
            { PaymentPlanList.PaymentPlan_Two, "Two payments: first payment of 50% due up front, the balance of 50% due within 30 days (increases fee by $100)." },
            };
            static string ConvertPaymentPlan(PaymentPlanList paymentplanlist)
            {
                string name;
                return (paymentplanDictionary.TryGetValue(paymentplanlist, out name))
                    ? name : paymentplanlist.ToString();
            }
            static开发者_JAVA百科 void Main()
            {
                Console.WriteLine(ConvertPaymentPlan(PaymentPlanList.PaymentPlan_One));
                Console.WriteLine(ConvertPaymentPlan(PaymentPlanList.PaymentPlan_Two));
            }
        }

And, for completeness, this is my view related to the above:

<p>
    @Html.RadioButtonFor(m => m.PaymentPlan, "PaymentPlan_One")
    One full payment in advance (receive the lowest price).
</p>
<p>
    @Html.RadioButtonFor(m => m.PaymentPlan, "PaymentPlan_Two")
    Two payments: first payment 50% due up front, the balance of 50% due within 30 days (increases fee by $100).
</p>

This is a quote system I have users fill out. For this particular service, say I charge $1,000.00. This is the base price. Based on user input, this price will be changed, and I want to show that to the user. So, if the user selects the first option, the price remains unchanged. If the user selects the second option, the fee is increased by $100.00.

This changes exponentially, since there are more inputs that affect the price (if selected).

Ultimately, based on the user inputs, I need to calculate the total. I am rendering a view which will display the total. I was thinking of using some @{} blocks and if/else if statements to either a) show nothing if what was selected does not increase the total, or b) showing the additional amount (e.g., $100.00), and then later showing a total.

Something like (EDITING here for clarity):

  • Base service: $1,000.00
  • Addon service1: $100.00 (only if user selects "PaymentPlan_Two" for two payments of 50% each (from the PaymentPlanList enum), otherwise hidden (and no addition of the $100.00) if user selects "PaymentPan_One" and pays in full)
  • Addon service2: $0.00 (this is hidden and a $0.00 or no value since the user did not select anything from a separate enum, but the value of $100.00 would be added if selected, which would make the Total $1,200.00 if it were selected; ALTERNATIVELY, how could I handle if there were 3 or more items in the list? E.g., Choice_One is $0.00, Choice_Two is $100.00 and Choice_Three is $200.00)
  • TOTAL: $1,100.00

Thanks for any help.


let's see if I understand your requirements correctly:

  • The application needs to add prices to a base price, depending on selection of Addon services.
  • These selections are from a Dictionary, which is based on an Enum
  • Therefore we're looking to store the price against the Enum to keep the data associations in one place.

It is possible to store a single value against an Enum:

    public enum PaymentPlanList
    {
        PaymentPlan_One = 100,
        PaymentPlan_Two = 200,
    }

However, I don't think this would be flexible enough for our needs - Enums only allow integers, and are commonly used this way in bitwise operations (where the values are multiples of 2).

I think a better solution here might be to use a Model-View-View-Model (MVVM) which can contain the logic about which services are available, how much they cost, and which services are valid in combination with other services.

There's an ticket pricing example (which sounds similar in concept to the domain here) on the Knockout.js home page that re-calculates a travel ticket price on the client web-page based on a user selection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜