2012-05-22 28 views
0

我試圖執行某種條件語句來計算值。爲了模擬我的數據,我在我的控制器(暫時)中分配了值,以查看我的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,並且條件將失敗。

這裏是 「模擬」 控制器:

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即將到期。

任何想法/指導?

以防萬一,這裏是一家位於StepFilingInformation.csPaymentPlanRadioButton(和我的車型之一):

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

您的Calculations類基於它包含的StepFilingInformation對象的數學基礎。但是,您從未將Calculate內的StepFilingInformation設置爲除新的空對象之外的任何內容。

您的構造函數可能需要一個StepFilingInformation類型的參數。不管你如何通過你的計算類StepFilingInformation參考

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

,必須之前你運行你的計算依賴於它將該值設置

此外,如果QuoteData是您的視圖模型,那麼它不應該包含你的計算類的引用。它只應包含視圖必須顯示的計算類中創建的結果。

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

這最後的代碼塊是什麼在我的控制器。所以'StepFilingInformation備案....'(前4行第二塊)實際上是在我的模擬控制器'QuoteData quoteData =新QuoteData ...'內。 – REMESQ

+1

是的,我看到了。你的模擬控制器創建計算類的一個實例,計算總而沒有設置備案與計算類,然後設置備案具有內QuoteData正確的數據計算結束後的第二個實例。 – DMulligan

+0

我明白你的意思了。我在'QuoteData quoteData'後結束嘗試'Calculations'和'VAR total'但不能正常工作。無論如何,真正的問題是我在'QuoteData'裏面有'StepFilingInformation'。好了,但現在,你在哪裏得到'總= total',因爲VS是抱怨'QuoteData'不包含「總」的定義? – REMESQ

相關問題