我正在尝试使用MvcMailer在我的MVC 3 Web应用程序中发送表单的内容。电子邮件发送,但不填充表单中的数据。
以下是我的表格视图:
@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>
}
这是我的控制器的一部分:
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");
}
这是我的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;
}
我的IApplicationMailer.cs:
public interface IApplicationMailer
{
MailMessage Application(Application application);
}
我的Application.cshtml:
@model DFPProductions_Default.Models.Application
Hi @ViewBag.Data
A new application has been received:
@ViewBag.FirstName
@ViewBag.LastName
修改
在包含表单的视图顶部,我有:
@model DFPProductions_Default.Models.Application
Application.cs是:
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;}
}
}
答案 0 :(得分:0)
我认为问题在于:
<a href='@Url.Action("SendApplication", "Home")'><input type="submit" value="Submit" /></a>
这不起作用。完全删除标签,只保留输入标签。然后将操作和控制器名称分配给表单,如下所示:
@Html.BeginForm("SendApplication", "Home", FormMethod.Post)