试图传递一个参数,但得到一个"上下文"错误

时间:2012-05-24 23:05:44

标签: c# asp.net-mvc

我试图将这个从我的控制器传递到我的视图(@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:

这是我的ViewModel(QuoteData):

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

1 个答案:

答案 0 :(得分: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被使用过。

无论如何,这只是我如何做的一个示例,创建一个特定于视图的模型,包括我需要的任何和所有属性,在控制器中填充模型并将其发送到视图

更新

根据OP注释,quoteData是一个ViewModel,如上所述,通过添加...来添加新属性以保存额外数据很简单...

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)