REMESQ
REMESQ

Reputation: 1200

Trying to pass calculated values to a view without a controller

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("[email protected]");

        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?)

Upvotes: 0

Views: 217

Answers (1)

mreyeros
mreyeros

Reputation: 4379

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("[email protected]");

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

    return mailMessage;
}

Upvotes: 1

Related Questions