Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果/Else或“如果”?:&引用;(有条件的)不工作_C#_Asp.net Mvc - Fatal编程技术网

C# 如果/Else或“如果”?:&引用;(有条件的)不工作

C# 如果/Else或“如果”?:&引用;(有条件的)不工作,c#,asp.net-mvc,C#,Asp.net Mvc,试图在此处重新表述/澄清问题: 我试图用某种条件语句来计算一个值。为了模拟我的数据,我(暂时)在我的控制器中赋值,以查看我的UI是如何运行的。我可以在视图中的功能块中执行计算,但它很长,不属于那里。因此,我现在尝试在一个模型中进行计算(Calculations.cs) 计算代码在传递值的情况下工作,但我的条件失败并传递默认值0,此时它应根据我在控制器中的模拟值传递另一个值 这是计算结果。cs public class Calculations { PriceQuote price = n

试图在此处重新表述/澄清问题:

我试图用某种条件语句来计算一个值。为了模拟我的数据,我(暂时)在我的控制器中赋值,以查看我的UI是如何运行的。我可以在视图中的功能块中执行计算,但它很长,不属于那里。因此,我现在尝试在一个模型中进行计算(
Calculations.cs

计算代码在传递值的情况下工作,但我的条件失败并传递默认值
0
,此时它应根据我在控制器中的模拟值传递另一个值

这是计算结果。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);
        }

    }
}
我最初有
(fileing.PaymentPlanRadioButton==Models.StepFilingInformation.PaymentPlan.Yes)
检查单选按钮是否设置为“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
):

这是我的ViewModel:

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
中(这是我的一款型号):


因此,我一直在努力将其放入
.cs
文件中。

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

如果是,则可能需要覆盖相等运算符(即,“==”测试可能失败)-有关教程,请参阅此处的MSDN文章:


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

如果是,则可能需要覆盖相等运算符(即,“==”测试可能失败)-有关教程,请参阅此处的MSDN文章:


您真的有这样的代码吗:

public decimal calculatingTest(MyModel MyModel)
{ ... }
而不是这个

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

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

你真的有这样的代码吗:

public decimal calculatingTest(MyModel MyModel)
{ ... }
而不是这个

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

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

在你的计算课上,你得到的返回值是“100”,而不是因为

MyModel.MyProperty == MyModel.MyPropertyEnum.Yes
但是因为它不等于MyModel.MyPropertyEnum.No。因此,您的if块刚好落在默认情况下。在这段代码中,MyModel.MyProperty没有赋值,所以它只接受默认值,从我所知道的是

(MyPropertyEnum?)null
“并非所有代码路径都返回值”异常是因为您已将MyProperty设置为可为null的类型,并且未为其提供结果

MyModel.MyProperty == null
同样,使用类型名作为变量名是一个糟糕的选择。Calculations.cs中有一个名为“MyModel”的类实例变量,CalculationTest中有一个名为MyModel的方法参数。这令人困惑

最后,StackOverflowException的原因是,您使用相同的参数递归调用CalculationTest,因此它进入无限循环


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

在您的计算类中,返回值为“100”,这不是因为

MyModel.MyProperty == MyModel.MyPropertyEnum.Yes
但是因为它不等于MyModel.MyPropertyEnum.No。因此,您的if块刚好落在默认情况下。在这段代码中,MyModel.MyProperty没有赋值,所以它只接受默认值,从我所知道的是

(MyPropertyEnum?)null
“并非所有代码路径都返回值”异常是因为您已将MyProperty设置为可为null的类型,并且未为其提供结果

MyModel.MyProperty == null
同样,使用类型名作为变量名是一个糟糕的选择。Calculations.cs中有一个名为“MyModel”的类实例变量,CalculationTest中有一个名为MyModel的方法参数。这令人困惑

最后,StackOverflowException的原因是,您使用相同的参数递归调用CalculationTest,因此它进入无限循环


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

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

我认为这里的根本问题是您实现计算类的方式。Chapter7Calculation属性总是返回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实例,您应该会看到更好的结果。例