如何使用MVC 5 C将表单中的值传递到电子邮件正文中#

时间:2016-01-30 19:00:31

标签: c# asp.net-mvc forms asp.net-mvc-5

好的,我的网站上有联系表格。我有能力发送带有标签的电子邮件,但我似乎无法获得要发送的值。所以,基本上我的电子邮件看起来像

名字:

姓氏:

公司:

评论:

我只是想知道我做错了什么。

控制器

    [HttpGet]
    public IActionResult Contact()
    {
        ViewData["Message"] = "Contact Us TODAY!";            
        return View();
    }


    [HttpPost]
    public IActionResult SendEmail()
    {

        var NewContact = new Forms();
        string body = "First Name: " + NewContact.FirstName + "\n"
                            + "Last Name:" + NewContact.LastName + "\n"
                            + "Company:" + NewContact.Company + "\n\n"
                            + "Comments:" + NewContact.Comments;
        MailMessage mailBody = new MailMessage("Something@example.com", "Somethingelse@example.com");
        mailBody.Subject = "Contact Form";
        mailBody.Body = body;

        SmtpClient smtpClient = new SmtpClient("smtp.aol.com", 587);
        smtpClient.Credentials = new NetworkCredential()
        {
            UserName = UserNameHidden,
            Password = PasswordHidden
        };

        smtpClient.EnableSsl = true;
        smtpClient.Send(mailBody);

        return Redirect("Contact");
    }

查看页面

<form asp-controller="Home" asp-action="SendEmail" name="ContactUs" method="post" class="bs-example-form">
            <div class="row">

                <div class="row">
                    <div class="text-danger" asp-validation-summary="ValidationSummary.All">
                        <p></p>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label asp-for="FirstName">First Name:</label>
                            <input class="form-control" asp-for="FirstName" placeholder="First Name*" type="text" required />
                            <span asp-validation-for="FirstName" class="text-danger"></span>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label asp-for="LastName">Last Name:</label>
                            <input class="form-control" asp-for="LastName" placeholder="Last Name*" type="text" required />
                            <span asp-validation-for="LastName" class="text-danger"></span>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label asp-for="PhoneNumber">Phone:</label>
                            <input class="form-control" asp-for="PhoneNumber" type="tel" asp-format="000-000-0000" onloosefocus="format"  placeholder="Phone Number*" required />
                            <span asp-validation-for="PhoneNumber" class="text-danger"></span>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label asp-for="Email">Email:</label>
                            <input class="form-control" asp-for="Email" type="email" placeholder="Email*" title="example@meems.org" required />
                            <span asp-validation-for="Email" class="text-danger"></span>
                        </div>
                    </div>
                </div>
                <span class="formspacer"></span><hr />
                <div class="row">
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label asp-for="Company">Company:</label>
                            <input class="form-control" asp-for="Company" placeholder="Company*" title="If you do not represent a company, please type 'NONE'." required />
                            <span asp-validation-for="Company" class="text-danger"></span>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label asp-for="StreetAddress">Street Address:</label>
                            <input class="form-control" asp-for="StreetAddress" placeholder="Street Address*" required />
                            <span asp-validation-for="StreetAddress" class="text-danger"></span>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label asp-for="City">City:</label>
                            <input class="form-control" asp-for="City" placeholder="City*" required />
                            <span asp-validation-for="City" class="text-danger"></span>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label asp-for="State">State:</label>
                            <input class="form-control" asp-for="State" placeholder="State*" required />
                            <span asp-validation-for="State" class="text-danger"></span>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label asp-for="ZipCode">Zip:</label>
                            <input class="form-control" asp-for="ZipCode" placeholder="Zip Code*" required />
                            <span asp-validation-for="ZipCode" class="text-danger"></span>
                        </div>
                    </div>
                        <div class="col-sm-12 bg-grey">
                            <div class="form-group">
                                <label asp-for="Comments">Comments:</label>
                                <input class="form-control" asp-for="Comments" placeholder="Comments" />
                            </div>
                        </div>
                </div>
                <div class="clearfix"></div>

                <div class="row">
                    <div class="col-sm-12">
                        <div class="buttonWrapper text-center">
                            <button class="btn btn-primary" type="reset" role="button">Reset</button>
                            <button class="btn btn-danger" type="submit" role="button" title="Coming Soon Please Just Email Us With This Information">Send</button>

模型

public class Forms
    {
        //Company Contact Information Customer
        public string Company { get; set; }
        [Required]
        public string FirstName { get; set; }
        [Required]
        public string LastName { get; set; }
        [Required]
        public string Email { get; set; }
        [Required]
        public long PhoneNumber { get; set; }
        public string Comments { get; set; }

        //Company Address Customer
        [Required]
        public string StreetAddress { get; set; }
        [Required]
        public string City { get; set; }
        [Required]
        public string State { get; set; }
        [Required]
        public string ZipCode { get; set; }




    }

3 个答案:

答案 0 :(得分:0)

您正在声明一个新的Forms对象,而不是将其传递给post方法中的参数。因此,这些字段都被初始化为默认值。

答案 1 :(得分:0)

看看这个。

[HttpPost]
public IActionResult SendEmail()
{
    var NewContact = new Forms();
   // Existing code
} 

您正在创建一个Forms类的全新对象。此新对象属性将具有默认值,而不是您发布的表单中的值。

您可以执行的操作是,向SendEmail类型的Forms操作方法添加参数。提交表单时,DefaultModelBinder会将表单字段映射到参数,因为属性名称与表单字段名称匹配。

[HttpPost]
public IActionResult SendEmail(Forms NewContact)
{       
   // Existing code which reads from NewContact object property values
}

答案 2 :(得分:0)

正如尼克所说,你需要将模型作为参数传递给你的控制器方法。

您还需要在使用其值之前检查模型的验证状态,并且(通常)如果模型无效则返回相同的视图,因此向用户显示验证错误。

所以你的方法可能是这样的:

[HttpPost]
public IActionResult SendEmail(Forms NewContact)
{
    if (ModelState.IsValid)
    {
        // model is valid, you can send email now

        string body = "First Name: " + NewContact.FirstName + "\n"
                            + "Last Name:" + NewContact.LastName + "\n"
                            + "Company:" + NewContact.Company + "\n\n"
                            + "Comments:" + NewContact.Comments;
        MailMessage mailBody = new MailMessage("Something@example.com", "Somethingelse@example.com");
        mailBody.Subject = "Contact Form";
        mailBody.Body = body;

        SmtpClient smtpClient = new SmtpClient("smtp.aol.com", 587);
        smtpClient.Credentials = new NetworkCredential()
        {
            UserName = UserNameHidden,
            Password = PasswordHidden
        };

        smtpClient.EnableSsl = true;
        smtpClient.Send(mailBody);

        return Redirect("Contact");
    }
    else
    {
        // model contains validation errors
        // return the same view so form is populated with existing values, 
        // and validation errors are shown
        //
        // You are responsible for populating model values in view page
        // it will be done automatically if you're using tag helpers like @Html.EditorFor()

        return View("Contact", model);
    }
}

同时发送邮件可能需要一些时间,因此您最好使控制器方法异步。