値を計算するために、ある種の条件文を実行しようとしています。データをモックするために、コントローラーで(一時的に)値を割り当てて、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です。
これを修正する方法がわかりません。モデルに割り当てて動作させると、モッキングコントローラーを取り外してユーザーがラジオボタンを選択することを期待した場合のように問題は解決しません。それでも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
は、コントローラーのラジオボタンに値として割り当てられているためです。PaymentPlanRadioButton
しかし、代わりに、デバッグ時にnullが発生するため、799(799 + 0)になります。
何か考え/ガイダンスはありますか?
念のため、ここにPaymentPlanRadioButton
ありますStepFilingInformation.cs
(そして私のモデルの1つです):
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; }
長さでごめんなさい。