3

我正在开发一个 MVC 3 应用程序,我正在使用 MvcMailer 发送电子邮件。我能够发送基本的电子邮件,但我正在尝试通过电子邮件发送表单的内容,但似乎无法解决。

这是我的模型的代码:

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;}


}

}

在我看来,我有编辑器字段,这里有一些:

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

我已经打电话给电子邮件应用程序。ApplicationMailer 的代码是:

    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;
    }

我的控制器中有这个:

    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");
    }

我添加到电子邮件视图中的只是

@model DFPProductions_Default.Models.Application

电子邮件发送,因此配置正确,但我真的不确定如何检索插入到电子邮件视图中的表单中的值。

谢谢,艾米

4

1 回答 1

0

您需要在控制器中使用您的模型类数据,如下所示:

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;
    }
于 2011-12-07T16:47:30.593 回答