我試圖執行某種條件語句來計算值。爲了模擬我的數據,我在我的控制器(暫時)中分配了值,以查看我的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.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; }
很抱歉的長度。
這最後的代碼塊是什麼在我的控制器。所以'StepFilingInformation備案....'(前4行第二塊)實際上是在我的模擬控制器'QuoteData quoteData =新QuoteData ...'內。 – REMESQ
是的,我看到了。你的模擬控制器創建計算類的一個實例,計算總而沒有設置備案與計算類,然後設置備案具有內QuoteData正確的數據計算結束後的第二個實例。 – DMulligan
我明白你的意思了。我在'QuoteData quoteData'後結束嘗試'Calculations'和'VAR total'但不能正常工作。無論如何,真正的問題是我在'QuoteData'裏面有'StepFilingInformation'。好了,但現在,你在哪裏得到'總= total',因爲VS是抱怨'QuoteData'不包含「總」的定義? – REMESQ