Reputation: 1200
Trying to rephrase/clean up question here:
I'm trying to do some sort of conditional statement to calculate a value. To mock my data I am assigning the value in my controller (temporarily) to see how my UI is coming along. I can perform the calculation in a function block in the view, but it's lengthy and doesn't belong there. So, I am trying now to do the calculation in a model (Calculations.cs
).
The code for the calculation is working in that a value is being passed, except that my condition is failing and passing the default value of 0
when it should be passing another value based on my mocked value in the controller.
Here is the Calculations.cs
public class Calculations
{
PriceQuote price = new PriceQuote();
StepFilingInformation filing = new StepFilingInformation();
public decimal Chapter7Calculation
{
get
{
return
price.priceChapter7
+
((filing.PaymentPlanRadioButton ==
Models.StepFilingInformation.PaymentPlan.Yes)
?
price.pricePaymentPlanChapter7
:
0);
}
}
}
I originally had (filing.PaymentPlanRadioButton == Models.StepFilingInformation.PaymentPlan.Yes)
checking whether or not the radio button was set to "Yes", but changed it to ReferenceEquals
. This doesn't affect the outcome.
I have my controller assigning the value to PaymentPlanRadioButton
to "Yes", so pricePaymentPlanChapter7
should be the value being added to priceChapter7
, but it is not. Instead "0" is being added as the fall back to the condition. So PaymentPlanRadioButton
is null even though I am assigning it in the controller.
I cannot figure out how to fix this. If I assign it in the model and get it to work that will not resolve the issue as when I remove the mocking controller and expect a user to choose a radio button it will still be null
and the condition will fail.
Here is the "mock" controller:
public class QuoteMailerController : Controller
{
public ActionResult EMailQuote()
{
Calculations calc = new Calculations();
var total = calc.Chapter7Calculation;
QuoteData quoteData = new QuoteData
{
StepFilingInformation = new Models.StepFilingInformation
{
//"No" is commented out, so "Yes" is assigned
//PaymentPlanRadioButton =
//Models.StepFilingInformation.PaymentPlan.No,
PaymentPlanRadioButton =
Models.StepFilingInformation.PaymentPlan.Yes,
}
};
}
}
And this is where I store prices (PriceQuote.cs
):
public class PriceQuote
{
public decimal priceChapter7 { get { return 799; } }
public decimal pricePaymentPlanChapter7 { get { return 100; } }
}
This is my ViewModel:
public class QuoteData
{
public PriceQuote priceQuote;
public Calculations calculations;
public StepFilingInformation stepFilingInformation { get; set; }
public QuoteData()
{
PriceQuote = new PriceQuote();
Calculations = new Calculations();
}
}
So, the way this should work is 799 + 100 = 899, since PaymentPlan.Yes
is assigned as the value to the radio button in the controller. But instead I am getting just 799 (799 + 0) because when I debug PaymentPlanRadioButton
is coming up null.
Any thoughts/guidance?
Just in case, here is the PaymentPlanRadioButton
located within StepFilingInformation.cs
(and is one of my models):
public enum PaymentPlan
{
No,
Yes
}
public class PaymentPlanSelectorAttribute : SelectorAttribute
{
public override IEnumerable<SelectListItem> GetItems()
{
return Selector.GetItemsFromEnum<PaymentPlan>();
}
}
[PaymentPlanSelector(BulkSelectionThreshold = 3)]
public PaymentPlan? PaymentPlanRadioButton { get; set; }
Sorry for the length.
FOR CONTEXT THIS IS WHAT I WAS TRYING TO GET OUT OF DOING
I originally had this calculation code in a function block in my view. The calculation works fine there, but obviously very lengthy and not appropriate.
This is what my function block looked like (partially)
@{ Model.PriceQuote.calculationChapter7
=
Model.PriceQuote.priceChapter7
+
((Model.StepFilingInformation.PaymentPlanRadioButton ==
StepFilingInformation.PaymentPlan.No)
?
Model.PriceQuote.priceNoPaymentPlan
:
Model.PriceQuote.pricePaymentPlanChapter7)
+
...//more of the same
;
}
So I've been struggling to get this into a .cs
file.
Upvotes: 1
Views: 297
Reputation: 6527
[Adding a 2nd answer since the added code has changed significantly since the original post].
I think the root problem here is the way you have implemented your Calculations class. The Chapter7Calculation property is always going to return 799 + 0, because it is using private local class variables to determine what values to return;
public class Calculations
{
PriceQuote price = new PriceQuote();
// private local variable - will ALWAYS have PaymentPlanRadioButton = null
StepFilingInformation filing = new StepFilingInformation();
public decimal Chapter7Calculation
{
get {
return
price.priceChapter7
+
(filing.PaymentPlanRadioButton == Models.StepFilingInformation.PaymentPlan.Yes)
? price.pricePaymentPlanChapter7
: 0);
}
}
}
You have a 'controller' modifying some other instance of StepFilingInformation, which your Calculations class has no awareness of. As well, your PriceQuote class is just returning constant or static values, so there's no real need to instantiate it. Modify that class like so;
public static class PriceQuote
{
public static decimal PriceChapter7 { get { return 799; } }
public static decimal PricePaymentPlanChapter7 { get { return 100; } }
}
Change your Calculations to a method like so;
public decimal CalculatePrice(QuoteData quoteData)
{
return PriceQuote.PriceChapter7 +
(quoteData.StepFilingInformation.PaymentPlanRadioButton == Models.StepFilingInformation.PaymentPlan.Yes)
? PriceQuote.PricePaymentPlanChapter7 : 0);
}
And now your controller can pass in the QuoteData instance it has created, and you should see a better result. Example code for the mock controller;
public class QuoteMailerController : Controller
{
public ActionResult EMailQuote()
{
Calculations calc = new Calculations();
QuoteData quoteData = new QuoteData
{
StepFilingInformation = new Models.StepFilingInformation
{
PaymentPlanRadioButton = Models.StepFilingInformation.PaymentPlan.Yes,
}
};
var total = calc.CalculatePrice(quoteData);
}
}
Upvotes: 1
Reputation: 6527
In your Calculations class, you are getting '100' as a return value not because
MyModel.MyProperty == MyModel.MyPropertyEnum.Yes
but because it doesn't equal MyModel.MyPropertyEnum.No. So your if block is just falling to the default case. In this code snipped, MyModel.MyProperty has no value assigned, so it just takes the default, which from what I can tell is;
(MyPropertyEnum?)null
The 'not all code paths return a value' exception is due to the fact you have made MyProperty a nullable type and not provided an outcome for;
MyModel.MyProperty == null
And again, your use of type names as variable names is a poor choice. You have a class instance variable called 'MyModel' in Calculations.cs and a method argument called MyModel in calculatingTest. It is confusing.
And lastly, the reason for your StackOverflowException is that you are recursively calling calculatingTest with the same argument, so it gets into an infinite loop.
There's quite a few issues here which you can clean up and you'll probably find the cause of your problem.
Upvotes: 1
Reputation: 14941
Have you really got this bit of code:
public decimal calculatingTest(MyModel MyModel)
{ ... }
rather than this?
public decimal calculatingTest(MyModel myModel) // note the difference in case.
{ ... }
Only that sort of thing can make life really complicated.
Upvotes: 3
Reputation: 6627
Have you tried debugging through to ensure that MyModel.MyModelProperty really does equal "No" at runtime?
If it is, perhaps you need to override the equality operator (i.e. perhaps the "==" test is failing) - see MSDN article here for a tutorial:
http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx
Upvotes: 1