1

これをコントローラーからビュー ( ) に渡そうとしています@ViewBag.Chapter7Total:

ViewBag.Chapter7Total = calc.CalculatePrice(quoteData, Chapter7);

しかし、VSで「現在のコンテキストエラーに存在しません」というエラーが発生しています。

基本的に、2.Chapter7 または Chapter13 の間で使用する価格設定構造を決定する 2 番目のパラメーターを渡そうとしています。選択によって、計算を実行する 2 番目のパラメーターが決定されます。

ここに私の方法があります:

class Chapter
{
    public decimal PaymentPlan { get; set; }
    public decimal Price { get; set; }
}

public decimal decPaymentPlan(QuoteData quoteData, Chapter chapter)
{
    if (quoteData.StepFilingInformation.PaymentPlanRadioButton 
        == StepFilingInformation.PaymentPlan.No)
        return PriceQuote.priceNoPaymentPlan;
    else
        return chapter.PaymentPlan;
}

public decimal Calculate(QuoteData quoteData, Chapter chapter)
{
    decimal total = chapter.Price;
    total += this.decPaymentPlan(quoteData, chapter);

    return total;
}

static Chapter Chapter7 = new Chapter() { Price = 799.00m, PaymentPlan = 100.00m };

最後に、これは私のコントローラーです:

public ActionResult EMailQuote()
{
    Calculations calc = new Calculations();
    Chapter chap = new Chapter();

    QuoteData quoteData = new QuoteData
    {
        StepFilingInformation = new Models.StepFilingInformation
        {
            //just moking user input here temporarily to test out the UI
            PaymentPlanRadioButton = Models.StepFilingInformation.PaymentPlan.Yes,
        }
     };

     var total = calc.CalculatePrice(quoteData);
     ViewBag.Chapter7Total = calc.CalculatePrice(quoteData, Chapter7);
     return View(quoteData);
}

Chapter7に合格するために何をすればいいのかわからない。何かご意見は?

更新 1:

これは私のViewModel(QuoteData)です:

public class QuoteData
{
    public PriceQuote priceQuote;
    public Calculations calculations;
    public StepFilingInformation stepFilingInformation { get; set; }
    public QuoteData()
    {
        PriceQuote = new PriceQuote();
        Calculations = new Calculations();
    }
}
4

1 に答える 1

1

私はあなたがここで何をしているのか把握しようとしていますが、最も重要なのは、あなたがquoteDataビューに送信していることです。ここで推測していますが、QuoteDataはあなたのカスタム エンティティ タイプであり、ViewModel.

まず、必要なQuoteDataViewModelすべてのプロパティを含む をモデルに作成します。QuoteData

public class QuoteDataViewModel {
   ... all of your quoteData properties here
   public Chapter Chapter7 { get; set; }
}

あなたのEMailQuote行動では、これに似た何か

public ActionResult EMailQuote() {
    ...
    var model = new QuoteDataViewModel();
    var quoteData = new QuoteData();
    ... // map your quoteData to your model with Automapper or manually like
    ... // model.SomeProperty = quoteData.SomeProperty;
    ... // repeat for all properties
    model.Chapter7 = Chapter7;
    return View(model);
}

このデータを再度投稿する場合はPost、新しいデータを受け入れるためのアクションが必要になりますQuoteDataViewModel

public ActionResult EmailQuote(QuoteDataViewModel model) {
    if(ModelState.IsValid) {
        ....//save data that was entered?
    }
    return View(model);
}

あなたのビューは QuoteDateViewModel を取ります

@model QuoteDataViewModel

これはすべて私が個人的に行う方法です。たとえば、次の行で何が起こっているのかよくわかりません。

var total = calc.CalculatePrice(quoteData);

totalあなたがそれを作成した後、使用されたことはありません。

とにかく、それは私がそれを行う方法の単なるサンプルであり、ビューに固有のモデルを作成し、必要なすべてのプロパティを含め、コントローラーにモデルを入力してビューに送信します

アップデート

quoteData が ViewModel であるという OP コメントに基づいて、上記と同様に、追加のデータを保持する新しいプロパティを追加するのは簡単です。

public decimal QuoteTotal { get; set; }
public Chapter Chapter7 { get; set; }

...ViewModel へ

コントローラーが入力されます

var total = calc.CalculatePrice(quoteData);
model.QuoteTotal = total;
model.Chapter7 = new Chapter();
model.Chapter7 = Chapter7;

ビューでは、次のように値にアクセスできます。

@Html.DisplayFor(model => model.QuoteTotal)
@Html.DisplayFor(model => model.Chapter7.PaymentPlan)
@Html.DisplayFor(model => model.Chapter7.Price)
于 2012-05-25T01:35:31.083 に答える