ここで質問を言い換え/クリーンアップしようとしています:
値を計算するために、ある種の条件文を実行しようとしています。データをモックするために、コントローラに (一時的に) 値を割り当てて、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
は、コントローラーのラジオ ボタンに値として割り当てられているためです。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; }
長々とすみません。
コンテキストについては、これは私がやろうとしていたことです
私はもともとこの計算コードをビューの関数ブロックに持っていました。計算はそこでうまくいきますが、明らかに非常に長く、適切ではありません。
これは私の機能ブロックがどのように見えるかです(部分的に)
@{ Model.PriceQuote.calculationChapter7
=
Model.PriceQuote.priceChapter7
+
((Model.StepFilingInformation.PaymentPlanRadioButton ==
StepFilingInformation.PaymentPlan.No)
?
Model.PriceQuote.priceNoPaymentPlan
:
Model.PriceQuote.pricePaymentPlanChapter7)
+
...//more of the same
;
}
だから私はこれを.cs
ファイルに入れるのに苦労してきました。