即使我在控制器中指定了值,值也显示为null

时间:2012-05-22 18:49:11

标签: asp.net-mvc model

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

计算的代码正在传递一个值,除了我的条件失败并传递默认值0时它应该根据我在控制器中的模拟值传递另一个值

以下是Calculations.cs

public class Calculations
{
    PriceQuote price = new PriceQuote();
    StepFilingInformation filing = new StepFilingInformation();
    public decimal Chapter7Calculation
    {
        get
        {
            return
                price.priceChapter7
                +
                ((ReferenceEquals
                    (filing.PaymentPlanRadioButton,
                    Models.StepFilingInformation.PaymentPlan.Yes))
                ?
                price.pricePaymentPlanChapter7
                :
                0);
        }

    }
}

我最初(filing.PaymentPlanRadioButton == Models.StepFilingInformation.PaymentPlan.Yes)检查单选按钮是否设置为“是”,但是将其更改为ReferenceEquals。这不会影响结果。

我让我的控制器将值PaymentPlanRadioButton指定为“是”,因此pricePaymentPlanChapter7应该是添加到priceChapter7的值,但事实并非如此。而是在回退到条件时添加“0”。因此即使我在控制器中分配它,PaymentPlanRadioButton也为空。

我无法弄清楚如何解决这个问题。如果我在模型中分配它并使其工作无法解决问题,因为当我删除模拟控制器并期望用户选择单选按钮时它仍然是null并且条件将失败。 / p>

这是“模拟”控制器:

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

这是我的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时出现了null。

有任何想法/指导吗?

以防万一,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; }

抱歉这个长度。

1 个答案:

答案 0 :(得分:1)

您的Calculations类将其数学基于它包含的StepFilingInformation对象。但是,您从未在计算中将StepFilingInformation设置为除新的空对象之外的任何内容。

您的构造函数可能需要一个StepFilingInformation类型的参数。

public class Calculations
{
    StepFilingInformation filing;
    public Calculations(StepFilingInformation filing)
    {
       this.filing = filing;
    } 

无论您如何向计算类传递对StepFilingInformation的引用,您必须在之前设置此值,然后运行依赖于它的计算。

此外,如果QuoteData是您的ViewModel,则它不应包含对Calculations类的引用。它应该只包含View必须显示的计算类中创建的结果。

StepFilingInformation filing = new Models.StepFilingInformation
{
    PaymentPlanRadioButton = Models.StepFilingInformation.PaymentPlan.Yes,
};

Calculations calc = new Calculations(filing);
var total = calc.Chapter7Calculation;

QuoteData quoteData = new QuoteData //Only include Properties you're going to display in the view model
{
    Total = total
};