试图在这里改写/清理问题:
我正在尝试做某种条件语句来计算一个值。为了模拟我的数据,我在我的控制器中(临时)分配值以查看我的 UI 是如何进行的。我可以在视图中的功能块中执行计算,但它很长并且不属于那里。所以,我现在正在尝试在模型(Calculations.cs
)中进行计算。
计算代码的工作原理是传递一个值,除了我的条件失败并传递了默认值,0
即它应该根据我在控制器中的模拟值传递另一个值的时间。
这里是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);
}
}
}
我最初(filing.PaymentPlanRadioButton == Models.StepFilingInformation.PaymentPlan.Yes)
检查单选按钮是否设置为“是”,但将其更改为ReferenceEquals
. 这不影响结果。
我让我的控制器将值分配PaymentPlanRadioButton
给“是”,因此pricePaymentPlanChapter7
应该将值添加到priceChapter7
,但事实并非如此。相反,添加“0”作为回退到条件。PaymentPlanRadioButton
即使我在控制器中分配它也是 null 。
我无法弄清楚如何解决这个问题。如果我在模型中分配它并让它工作,这将无法解决问题,因为当我删除模拟控制器并期望用户选择一个单选按钮时,它仍然会是null
并且条件将失败。
这是“模拟”控制器:
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,
}
};
}
}
这是我存储价格的地方(PriceQuote.cs
):
public class PriceQuote
{
public decimal priceChapter7 { get { return 799; } }
public decimal pricePaymentPlanChapter7 { get { return 100; } }
}
这是我的视图模型:
public class QuoteData
{
public PriceQuote priceQuote;
public Calculations calculations;
public StepFilingInformation stepFilingInformation { get; set; }
public QuoteData()
{
PriceQuote = new PriceQuote();
Calculations = new Calculations();
}
}
因此,这应该工作的方式是 799 + 100 = 899,因为PaymentPlan.Yes
它被分配为控制器中单选按钮的值。但相反,我得到的只是 799 (799 + 0),因为当我调试时PaymentPlanRadioButton
出现空值。
有什么想法/指导吗?
以防万一,这是PaymentPlanRadioButton
位于内部StepFilingInformation.cs
(并且是我的模型之一):
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; }
对不起,长度。
对于上下文,这是我试图摆脱的
在我看来,我最初在一个功能块中有这个计算代码。计算在那里工作正常,但显然非常冗长且不合适。
这就是我的功能块的样子(部分)
@{ Model.PriceQuote.calculationChapter7
=
Model.PriceQuote.priceChapter7
+
((Model.StepFilingInformation.PaymentPlanRadioButton ==
StepFilingInformation.PaymentPlan.No)
?
Model.PriceQuote.priceNoPaymentPlan
:
Model.PriceQuote.pricePaymentPlanChapter7)
+
...//more of the same
;
}
因此,我一直在努力将其写入.cs
文件。