3

I am developing an MVC 3 application and I am sending emails using MvcMailer.I am able to send basic emails, but I am trying to email the contents of a form and can't seem to work it out.

Here is the code for my model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace DFPProductions_Default.Models
{
public class Application
{
    [Required]
    [Display(Name = "First Name")]
    public string ApplicantFirstName { get; set; }
    [Required]
    [Display(Name = "Last Name")]
    public string ApplicantLastName { get; set; }
    [Display(Name = "Birth Date")]
    public DateTime ApplicantBirthDate { get; set; }
    [Required]
    [Display(Name = "Cellphone Number")]
    public string ApplicantCellphoneNumber { get; set; }
    [Display(Name = "Postal Address")]
    public string PostalNumber { get; set; }
    [Display(Name = "Suburb")]
    public string ApplicantSuburb { get; set; }
    [Display(Name = "City")]
    public string ApplicantCity { get; set; }
    [Display(Name = "Post Code")]
    public string ApplicationPostalCode { get; set; }
    [Required]
    [Display(Name = "Email Address")]
    public string ApplicantEmailAddress { get; set; }

    [Display(Name = "First Name")]
    public string ParentFirstName { get; set; }
    [Display(Name = "Last Name")]
    public string ParentLastName { get; set; }
    [Display(Name = "Email Address")]
    public string ParentEmailAddress { get; set; }
    [Display(Name = "Postal Address")]
    public string ParentPostalNumber { get; set; }
    [Display(Name = "Suburb")]
    public string ParentSuburb { get; set; }
    [Display(Name = "City")]
    public string ParentCity {get; set;}
    [Display(Name = "Post Code")]
    public string ParentPostalCode {get; set;}


}

}

In my view, I have editor fields, here are a few:

    <div class="editor-label">
        @Html.LabelFor(model => model.ApplicantFirstName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ApplicantFirstName)
        @Html.ValidationMessageFor(model => model.ApplicantFirstName)
    </div>


    <div class="editor-label">
        @Html.LabelFor(model => model.ApplicantLastName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ApplicantLastName)
        @Html.ValidationMessageFor(model => model.ApplicantLastName)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.ApplicantBirthDate)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ApplicantBirthDate)
        @Html.ValidationMessageFor(model => model.ApplicantBirthDate)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.ApplicantCellphoneNumber)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ApplicantCellphoneNumber)
        @Html.ValidationMessageFor(model => model.ApplicantCellphoneNumber)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.ApplicantEmailAddress)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ApplicantEmailAddress)
        @Html.ValidationMessageFor(model => model.ApplicantEmailAddress)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.PostalNumber)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.PostalNumber)
        @Html.ValidationMessageFor(model => model.ApplicantEmailAddress)
    </div>

     <div class="editor-label">
        @Html.LabelFor(model => model.ApplicantSuburb)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ApplicantSuburb)
        @Html.ValidationMessageFor(model => model.ApplicantSuburb)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.ApplicantCity)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ApplicantCity)
        @Html.ValidationMessageFor(model => model.ApplicantCity)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.ApplicationPostalCode)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ApplicationPostalCode)
        @Html.ValidationMessageFor(model => model.ApplicationPostalCode)
    </div>

</div> 

I have called the email Application. The code for ApplicationMailer is:

    public virtual MailMessage Application()
    {
        var mailMessage = new MailMessage{Subject = "Application"};

        mailMessage.To.Add("debbie@gmail.com");
        ViewBag.Data = "Debbie";
        PopulateBody(mailMessage, viewName: "Application");

        return mailMessage;
    }

I have this in my controller:

    private IApplicationMailer _applicationMailer = new ApplicationMailer();
    public IApplicationMailer ApplicationMailer
    {
        get { return _applicationMailer; }
        set { _applicationMailer = value; }
    }

    public ActionResult SendApplication()
    {
        ApplicationMailer.Application().Send();
         //Send() extension method: using Mvc.Mailer
        return RedirectToAction("Index");
    }

And all I've added to the view of the email is

@model DFPProductions_Default.Models.Application

The email sends, so it is configured correctly, but I'm really not sure how to retrieve the values inserted into the form in the email view.

Thanks, Amy

Amy
  • 503
  • 4
  • 9
  • 20
  • See the "Edit Your View" section in the [tutorial](https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide). Basically, you just print the values from the Model (the "values inserted into the form") in the e-mail View. – bzlm Oct 03 '11 at 20:54
  • Hey, thanks. I did see that section in the tutorial and I was able to use email addressed stored in the database. But does this mean that I would have to save the Application to the database first before sending the email? – Amy Oct 03 '11 at 21:04
  • Possibly; didn't read your wall of code. When you do `ViewBag.Data = "Debbie";`, you're making the `Data` property available to the view, so that the view can print it, resulting in "Debbie". Looks like you can do the same for the other "values inserted into the form". – bzlm Oct 03 '11 at 21:07
  • Okay, I see what you're saying. It would be easy if I was calling the database entry, because I would just say var applicant= db.Applicant.FirstOrDefault(); and then applicant.ApplicantFullName or something like that. But if I want to send the email when the user presses 'Submit' and nothing actually saves to a DB, how do I retrieve the values? – Amy Oct 03 '11 at 21:21
  • There's no need for a database. You need to make sure the action method `SendApplication` actually receives the posted model (like `SendApplication(Application application)`) and then pass the model along to the mailer; for example as an argument to the `Application()` method. Then inside that method, you can fill the `ViewBag` from the argument. So the application comes into the action method, is passed to the mailer, which populates the view, and then sends the rendered view as an e-mail. – bzlm Oct 03 '11 at 21:28
  • Okay, I'm trying that now. I've added (Application application) to the SendApplication and I have a button on the form like this: I'm not sure if it's right to use the Action? But when I debug, application is all null. – Amy Oct 03 '11 at 21:53
  • do you still need help on this @Amy ? – Diin Mar 12 '15 at 06:28
  • No, all good thanks @Diin! – Amy Mar 18 '15 at 12:44

1 Answers1

0

You need to utilize your model class data in the controller something like this:

public virtual MailMessage Application(ModelNameHere model)
    {
        var mailMessage = new MailMessage{Subject = "Application"};

        mailMessage.To.Add("debbie@gmail.com");

        //add field from your view here
        string body = model.CellPhoneNumber + Model.FirstName;

        ViewBag.Data = "Debbie";
        PopulateBody(mailMessage, viewName: "Application", body);

        return mailMessage;
    }
bobek
  • 8,003
  • 8
  • 39
  • 75