1

ここで質問を言い換え/クリーンアップしようとしています:

値を計算するために、ある種の条件文を実行しようとしています。データをモックするために、コントローラに (一時的に) 値を割り当てて、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ファイルに入れるのに苦労してきました。

4

4 に答える 4

3

あなたは本当にこのコードを手に入れましたか:

public decimal calculatingTest(MyModel MyModel)
{ ... }

これよりも?

public decimal calculatingTest(MyModel myModel) // note the difference in case.
{ ... }

そのようなことだけが人生を本当に複雑にする可能性があります。

于 2012-05-21T23:02:58.507 に答える
1

[追加されたコードが元の投稿から大幅に変更されたため、2 番目の回答を追加]。

ここでの根本的な問題は、Calculations クラスの実装方法にあると思います。Chapter7Calculation プロパティは常に799 + 0 を返します。これは、返す値を決定するためにプライベート ローカル クラス変数を使用しているためです。

public class Calculations
{
    PriceQuote price = new PriceQuote();

    // private local variable - will ALWAYS have PaymentPlanRadioButton = null
    StepFilingInformation filing = new StepFilingInformation();

    public decimal Chapter7Calculation
    {
        get {
            return
                price.priceChapter7
                +
                (filing.PaymentPlanRadioButton == Models.StepFilingInformation.PaymentPlan.Yes)
                ? price.pricePaymentPlanChapter7
                : 0);
        }
    }
}

計算クラスが認識していない StepFilingInformationの他のインスタンスを変更する「コントローラー」があります。同様に、PriceQuote クラスは定数または静的な値を返すだけなので、インスタンス化する必要はありません。そのクラスを次のように変更します。

public static class PriceQuote
{
    public static decimal PriceChapter7 { get { return 799; } }
    public static decimal PricePaymentPlanChapter7 { get { return 100; } }
}

計算をそのような方法に変更します。

public decimal CalculatePrice(QuoteData quoteData)
{
    return PriceQuote.PriceChapter7 + 
        (quoteData.StepFilingInformation.PaymentPlanRadioButton == Models.StepFilingInformation.PaymentPlan.Yes) 
        ? PriceQuote.PricePaymentPlanChapter7 : 0);
}

これで、コントローラーが作成した QuoteData インスタンスを渡すことができるようになり、より良い結果が得られるはずです。モック コントローラーのサンプル コード。

public class QuoteMailerController : Controller 
{
    public ActionResult EMailQuote()
    {
        Calculations calc = new Calculations();

        QuoteData quoteData = new QuoteData
        {
            StepFilingInformation = new Models.StepFilingInformation
            {
                PaymentPlanRadioButton = Models.StepFilingInformation.PaymentPlan.Yes,
            }
         };

         var total = calc.CalculatePrice(quoteData);
    }
}
于 2012-05-22T23:01:37.827 に答える
1

あなたのCalculationsクラスでは、戻り値として「100」を取得しています。

MyModel.MyProperty == MyModel.MyPropertyEnum.Yes

しかし、それはMyModel.MyPropertyEnum.No と等しくないためです。したがって、 if ブロックはデフォルトのケースに陥っています。この抜粋されたコードでは、MyModel.MyProperty に値が割り当てられていないため、デフォルトが使用されます。

(MyPropertyEnum?)null

「すべてのコード パスが値を返すわけではない」例外は、MyProperty を null 許容型にし、結果を提供しなかったという事実によるものです。

MyModel.MyProperty == null

繰り返しになりますが、型名を変数名として使用するのは適切ではありません。Calculations.cs には 'MyModel' というクラス インスタンス変数があり、calculationTest には MyModel というメソッド引数があります。それは紛らわしい。

そして最後に、StackOverflowException の理由は、同じ引数で calculateTest を再帰的に呼び出しているため、無限ループに陥ることです。

ここには、クリーンアップできる問題がかなりあり、問題の原因が見つかる可能性があります。

于 2012-05-22T02:03:32.767 に答える
1

実行時に MyModel.MyModelProperty が実際に「いいえ」であることを確認するためにデバッグを試みましたか?

そうであれば、等値演算子を上書きする必要があるかもしれません (つまり、"==" テストが失敗している可能性があります) - チュートリアルについては、MSDN の記事を参照してください:

http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx

于 2012-05-21T22:55:03.093 に答える