English 中文(简体)
试图通过参数但获取“ context” 错误
原标题:Trying to pass a parameter but getting a "context" error

我试图把这个从我的控制器传到我的视野(7Captail ):

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章 有什么想法吗?

<强 > UPATE 1:

这是我的视图模式 ():

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

我试图弄清楚你在这里做什么,但我看到,最重要的是,你正在发送 qoteData 到您的视图中。我在这里猜测,但我发现 ooteData 是您的定制实体类型,而不是 ViewModel

首先,我要在您的模型中创建一个 ,该模型具有您需要的所有特性 ,包括

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

在您 email iuote 动作中,类似此动作

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 动作来接受新的

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

您的视图将使用引号DateView 模式

@model QuoteDataViewModel

我完全不明白你到底在说什么, 比如说,这条线:

var total = calc.CalculatePrice(quoteData);

我看不出在您创建后 曾经使用过 commed community

总之, 仅此样板, 我如何做它, 创建一个特定视图的模型, 包括任何和所有我需要的属性, 在控制器中填入模型, 并将其发送到视图中 。

Update

根据OP的评论,引用Data是视图模式,然后和上文一样,添加新属性以持有额外数据很简单,因为添加...

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)
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签