I am trying to send the contents of a form with MvcMailer in my MVC 3 web application. The email sends, but it does not populate with the data from the form.
Here is the view of my form:
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div id="section_1" class="section">
<p style="color:#e93738;display:none"></p>
<img src="/Content/1.png" id="one" class="step" alt="Step 1"/>
<h3>Personal Details of Student</h3>
<p>
<em>Please enter your personal details.</em><br />
</p>
<br />
<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>
<div id="section_2" class="section">
<img src="/Content/2.png" id="two" class="step" alt="Step 2"/>
<h3>Parent Details</h3>
<p>
<em>Please enter your parent or guardian's details.</em><br />
</p>
<div class="editor-label">
@Html.LabelFor(model => model.ParentFirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ParentFirstName)
@Html.ValidationMessageFor(model => model.ParentFirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ParentLastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ParentLastName)
@Html.ValidationMessageFor(model => model.ParentLastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ParentEmailAddress)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ParentEmailAddress)
@Html.ValidationMessageFor(model => model.ParentEmailAddress)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ParentPostalNumber)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ParentPostalNumber)
@Html.ValidationMessageFor(model => model.ParentPostalNumber)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ParentSuburb)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ParentSuburb)
@Html.ValidationMessageFor(model => model.ParentSuburb)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ParentCity)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ParentCity)
@Html.ValidationMessageFor(model => model.ParentCity)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ParentPostalCode)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ParentPostalCode)
@Html.ValidationMessageFor(model => model.ParentPostalCode)
</div>
</div>
<a href='@Url.Action("SendApplication", "Home")'><input type="submit" value="Submit" /></a>
}
Here is part of my controller:
private IApplicationMailer _applicationMailer = new ApplicationMailer();
public IApplicationMailer ApplicationMailer
{
get { return _applicationMailer; }
set { _applicationMailer = value; }
}
public ActionResult SendApplication(Application application)
{
ApplicationMailer.Application(application).Send();
//Send() extension method: using Mvc.Mailer
return RedirectToAction("Index");
}
Here is my ApplicationMailer.cs:
public virtual MailMessage Application(Application application)
{
var mailMessage = new MailMessage{Subject = "Application"};
mailMessage.To.Add("amecily@gmail.com");
ViewBag.Data = "Debbie";
ViewBag.FirstName = application.ApplicantFirstName;
ViewBag.LastName = application.ApplicantLastName;
PopulateBody(mailMessage, viewName: "Application");
return mailMessage;
}
My IApplicationMailer.cs:
public interface IApplicationMailer
{
MailMessage Application(Application application);
}
And my Application.cshtml:
@model DFPProductions_Default.Mo开发者_如何转开发dels.Application
Hi @ViewBag.Data
A new application has been received:
@ViewBag.FirstName
@ViewBag.LastName
EDIT:
At the top of the view containing the form, I have:
@model DFPProductions_Default.Models.Application
And the Application.cs is:
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;}
}
}
I think the problem lies here:
<a href='@Url.Action("SendApplication", "Home")'><input type="submit" value="Submit" /></a>
This won't work. Remove the tags entirely so only the input tag remains. Then assign the action and controllername to the form like this:
@Html.BeginForm("SendApplication", "Home", FormMethod.Post)
精彩评论