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です。

これを修正する方法がわかりません。モデルに割り当てて動作させると、モッキングコントローラーを取り外してユーザーがラジオボタンを選択することを期待した場合のように問題は解決しません。それでも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; }

長さでごめんなさい。

4

1 に答える 1

1

Calculationsクラスは、含まれているStepFilingInformationオブジェクトに基づいて計算されます。ただし、Calculations内のStepFilingInformationを新しい空のオブジェクト以外に設定することはありません。

コンストラクターには、おそらくStepFilingInformation型のパラメーターが必要です。

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

計算クラスにStepFilingInformationへの参照を渡す方法に関係なく、それに依存する計算を実行する前に、この値を設定する必要があります。

また、QuoteDataがViewModelである場合、Calculationsクラスへの参照を含めることはできません。ビューに表示する必要のある計算クラスで作成された結果のみが含まれている必要があります。

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
};
于 2012-05-22T20:00:30.687 に答える