尝试传递参数但收到“上下文”错误
本文关键字:上下文 错误 参数 | 更新日期: 2023-09-27 18:35:27
我正在尝试将其从我的控制器传递到我的视图中(@ViewBag.Chapter7Total
):
ViewBag.Chapter7Total = calc.CalculatePrice(quoteData, Chapter7);
但是我在VS中遇到"当前上下文中不存在错误"。
基本上,我正在尝试传入第二个参数,该参数确定在 2 之间使用哪种定价结构。第 7 章或第 13 章,选择确定要执行计算的第二个参数。
以下是我的方法:
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();
}
}
我试图弄清楚你在这里做什么,但我看到最重要的是,你正在将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; }
。到视图模型
控制器填充
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)