1

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

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

4

4 回答 4

3

你真的有这段代码吗:

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

而不是这个?

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

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

于 2012-05-21T23:02:58.507 回答
1

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

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

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

于 2012-05-21T22:55:03.093 回答
1

在您的 Calculations 类中,您得到 '100' 作为返回值不是因为

MyModel.MyProperty == MyModel.MyPropertyEnum.Yes

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

(MyPropertyEnum?)null

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

MyModel.MyProperty == null

同样,您将类型名称用作变量名称是一个糟糕的选择。在Calculations.cs 中有一个名为“MyModel”的类实例变量,在calculationTest 中有一个名为MyModel 的方法参数。这令人困惑。

最后,您的StackOverflowException的原因是您使用相同的参数递归调用计算测试,因此它进入了无限循环。

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

于 2012-05-22T02:03:32.767 回答
1

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

我认为这里的根本问题是您实现 Calculations 类的方式。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 的其他一些实例,而您的 Calculations 类不知道这些实例。同样,您的 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);
    }
}
于 2012-05-22T23:01:37.827 回答