表单中的数据未通过电子邮件发送 - MvcMailer

发布于 2024-12-08 10:32:13 字数 9001 浏览 11 评论 0原文

我正在尝试在 MVC 3 Web 应用程序中使用 MvcMailer 发送表单内容。电子邮件已发送,但未填充表单中的数据。

这是我的表单的视图:

@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("[email protected]");
        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;}


   }
}

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("[email protected]");
        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.Models.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;}


   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

知你几分 2024-12-15 10:32:14

我认为问题就出在这里:

<a href='@Url.Action("SendApplication", "Home")'><input type="submit" value="Submit" /></a>

这行不通。完全删除标签,以便仅保留输入标签。然后将操作和控制器名称分配给表单,如下所示:

@Html.BeginForm("SendApplication", "Home", FormMethod.Post)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文