如果/否则或“?:“(有条件)不起作用

本文关键字:有条件 不起作用 如果 | 更新日期: 2023-09-27 18:34:48

试图在这里改写/清理问题:

我正在尝试做某种条件语句来计算一个值。为了模拟我的数据,我在我的控制器中(临时(分配值,以查看我的 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并且条件会失败。

这是"模拟"控制器:

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出现空。

有什么想法/指导吗?

以防万一,这是位于StepFilingInformation.cs内的PaymentPlanRadioButton(并且是我的模型之一(:

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文件中。

如果/否则或“?:“(有条件)不起作用

你真的有这段代码吗:

public decimal calculatingTest(MyModel MyModel)
{ ... }

而不是这个?

public decimal calculatingTest(MyModel myModel) // note the difference in case.
{ ... }

只有这样的事情才能让生活变得真正复杂。

[添加第二个答案,因为添加的代码自原始帖子以来发生了重大变化]。

我认为这里的根本问题是你实现计算类的方式。Chapter7Computing 属性始终将返回 799 + 0,因为它使用私有局部类变量来确定要返回的值;

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);
        }
    }
}

你有一个"控制器"修改了 StepFilingInformation 的其他实例,而你的计算类没有意识到这些实例。同样,PriceQuote 类只是返回常量或静态值,因此实际上不需要实例化它。像这样修改该类;

public static class PriceQuote
{
    public static decimal PriceChapter7 { get { return 799; } }
    public static decimal PricePaymentPlanChapter7 { get { return 100; } }
}

将您的计算更改为这样的方法;

public decimal CalculatePrice(QuoteData quoteData)
{
    return PriceQuote.PriceChapter7 + 
        (quoteData.StepFilingInformation.PaymentPlanRadioButton == Models.StepFilingInformation.PaymentPlan.Yes) 
        ? PriceQuote.PricePaymentPlanChapter7 : 0);
}

现在,您的控制器可以传入它创建的 QuoteData 实例,您应该会看到更好的结果。 模拟控制器的示例代码;

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);
    }
}

您是否尝试过调试以确保 MyModel.MyModelProperty 在运行时确实等于"否"?

如果是,也许您需要覆盖相等运算符(即"=="测试可能失败( - 请参阅此处的 MSDN 文章以获取教程:

http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80(.aspx

在计算类中,你得到'100'作为返回值不是因为

MyModel.MyProperty == MyModel.MyPropertyEnum.Yes

而是因为它等于 MyModel.MyPropertyEnum.No。所以你的if块只是落到默认情况。在这段截取的代码中,MyModel.MyProperty 没有分配任何值,因此它只采用默认值,据我所知;

(MyPropertyEnum?)null

"并非所有代码路径都返回值"异常是由于您已将 MyProperty 设置为可为空的类型并且未提供结果;

MyModel.MyProperty == null

同样,使用类型名称作为变量名称是一个糟糕的选择。计算中有一个名为"MyModel"的类实例变量.cs在 calculatingTest 中有一个名为 MyModel 的方法参数。这是令人困惑的。

最后,StackOverflowException的原因是你使用相同的参数递归调用calculatingTest,所以它进入了一个无限循环。

这里有很多问题可以清理,您可能会找到问题的原因。