Email Form with MvcMailer
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.T开发者_JAVA百科o.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
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;
}
精彩评论