1

我试图将它从我的控制器传递到我的视图中(@ViewBag.Chapter7Total):

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

但是在 VS 中出现“当前上下文错误中不存在”。

基本上,我试图传入第二个参数,该参数确定在 2. Chapter7 或 Chapter13 之间使用哪种定价结构,选择确定第二个参数来执行计算。

这是我的方法:

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);
}

我不知道该怎么做才能通过第 7 章。有什么想法吗?

更新 1:

这是我的视图模型(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 回答