0

I am using MvcMailer and trying to pass calculated values. These values are calculated after the user inputs information into a wizard form (textbox, radio button list, etc.). Sorry if this is lengthy, but I want to give as much context as possible to avoid "show me more code" comments. I am using a View Model (QuoteData). My "prices" are kept in a model (PriceQuote).

Here is an example calculation (and the attendant "total" code) in my Calculation.cs:

public decimal decOne(QuoteData quoteData)
{
    if (quoteData.Step.RadioButtonOne == Step.EnumOne.ChoiceOne)
            return PriceQuote.priceOne;
    else
        return 0;
}
....
public decimal TotalOne(QuoteData quoteData)
{
    decimal totalOne = PriceQuote.priceBase;
    total += this.decOne(quoteData);
    ....
    return total;
}

My View Model is like this (not sure if this is correct, or if this is where I should do some calculations):

public class QuoteData
{
    public Calculations Calculations { get; set; }
    ....
}
public QuoteData()
{
Calculations = new Caluclations();
}

What I previously did for mock-up purposes was put this in an action in a mock controller:

Calculations calc = new Calculations();

var totalone = calc.TotalOne(quoteData);
ViewBag.CalculateTotalOne = totalone;

return View(quoteData);

And I called it thusly in the page:

@((ViewBag.CalculationTotalOne).ToString("C2"))

Again, this was for mock purposes. Now, with MvcMailer I cannot do the same thing, and have struggled all weekend to figure out how to get it to work. Everything I've researched and tried did not work. I'm sure it's something simple that I have overlooked.

MvcMailer's view doesn't use a controller. It uses a class (QuoteMailer in my case). Here is how my Wizard passes to MvcMailer:

public ActionResult Submitted()
{
    QuoteMailer.EMailQuote(quoteData).Send();
    return View(quoteData);
}

Here is EMailQuote in QuoteMailer:

public virtual MailMessage EMailQuote(QuoteData model)
    {
        var mailMessage = new MailMessage { Subject = "..." };

        mailMessage.To.Add(model.Step.EMail);
        mailMessage.To.Add("web@site.com");

        ViewData = new ViewDataDictionary(model);
        PopulateBody(mailMessage, viewName: "EMailQuote");

        return mailMessage;
    }

Any help is appreciated (how do I pass my calculations here, whereas in the mock scenario I used ViewBag?)

REMESQ
  • 1,190
  • 2
  • 26
  • 60

1 Answers1

1

MVCMailer Mailers extend ControllerBase so at the end of the day they work exactly like a controller. You should be able to send your data to your Mailer view the same way(s) you would in a regular controller. Here is a link that describes all the ways that you can send data to your Mailer View. In your case you may be able to do the following to use your strongly typed model as long as the View expects of Model of type QuoteData:

public virtual MailMessage EMailQuote(QuoteData model)
{
    var mailMessage = new MailMessage { Subject = "..." };

    mailMessage.To.Add(model.Step.EMail);
    mailMessage.To.Add("web@site.com");

    ViewData.Model = model;
    PopulateBody(mailMessage, viewName: "EMailQuote");

    return mailMessage;
}
mreyeros
  • 4,359
  • 20
  • 24
  • Assuming this is correct (the only change I see is `ViewData.Model = model;`), then is it just a matter of calling `@Calculations.CalculationTotalOne` in the View? – REMESQ Jul 30 '12 at 17:25
  • Well it would be @Model.Calculations.CalculationTotalOne, since your model is of type QuoteData and Calculations is a property of QuoteData. – mreyeros Jul 30 '12 at 17:39
  • Yeah, this is where i get messed up. Doing `@Model.Calculations.CalculationTotalOne` doesn't work. `Cannot convert method group 'TotalOne' to non-delegate type 'object'` is the error. (Note that I messed up in my first comment, it's not "CalculationTotalOne"). – REMESQ Jul 30 '12 at 20:29
  • Ok, I got it! Thanks for the help. Sorry about that last comment. – REMESQ Jul 30 '12 at 20:44
  • Ok I think that I understand what is going on...if your calculations class requires that you pass in the QuoteData to the TotalOne function then maybe you can make the Calculations class and its methods static. That way you can continue to pass in QuoteData as your model and in your view you would call @Calculations.TotalOne(Model) – mreyeros Jul 30 '12 at 20:47