【问题标题】:Email Form with MvcMailer使用 MvcMailer 的电子邮件表单
【发布时间】:2011-12-08 00:47:30
【问题描述】:

我正在开发一个 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

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

谢谢, 艾米

【问题讨论】:

  • 请参阅tutorial 中的“编辑您的视图”部分。基本上,您只需在电子邮件视图中打印模型中的值(“插入到表单中的值”)。
  • 嘿,谢谢。我确实在教程中看到了该部分,并且能够使用存储在数据库中的电子邮件地址。但这是否意味着我必须在发送电子邮件之前先将应用程序保存到数据库中?
  • 可能;没看过你的代码墙。当您执行ViewBag.Data = "Debbie"; 时,您使Data 属性可用于视图,以便视图可以打印它,从而产生“黛比”。看起来您可以对其他“插入表单的值”执行相同的操作。
  • 好的,我明白你在说什么。如果我调用数据库条目会很容易,因为我只会说 var application= db.Applicant.FirstOrDefault();然后是申请人.ApplicantFullName 或类似的东西。但是,如果我想在用户按下“提交”时发送电子邮件并且实际上没有任何内容保存到数据库中,我该如何检索这些值?
  • 不需要数据库。您需要确保操作方法SendApplication 实际接收到发布的模型(如SendApplication(Application application)),然后将模型传递给邮件程序;例如作为Application() 方法的参数。然后在该方法中,您可以从参数中填充ViewBag。所以应用程序进入 action 方法,传递给邮件程序,邮件程序填充视图,然后将呈现的视图作为电子邮件发送。

标签: c# asp.net-mvc-3 email mvcmailer


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    相关资源
    最近更新 更多