简体   繁体   English

ASP.NET Razor表单数据未传递到电子邮件中使用的变量

[英]ASP.NET Razor Form Data not being passed to variables used in e-mail

I'm just now experimenting with ASP.NET and Razor, I'm trying to take a simple HTML form and have it pass data to Variables on POST and use those variables to build a simple e-mail sent by SMTP. 我现在正在尝试使用ASP.NET和Razor,我试图采用一种简单的HTML形式,并使其将数据传递给POST上的变量,并使用这些变量来构建由SMTP发送的简单电子邮件。 However it appears that the data is not being passed to these variables as when the e-mail is sent they are blank. 但是,似乎没有将数据传递给这些变量,因为发送电子邮件时它们为空白。 If I hard-code values to these variables they get shown correctly. 如果我将值硬编码到这些变量,它们将正确显示。 Here is a snippet of the code I'm working with. 这是我正在使用的代码的片段。 Ignore the localhost under WebMail.SmtpServer, I'm using SMTP4dev to test the e-mail functionality locally. 忽略WebMail.SmtpServer下的localhost,我正在使用SMTP4dev在本地测试电子邮件功能。 This first section is the razor code: 第一部分是剃刀代码:

@{
var errorMessage = "";

if (IsPost)
{

    var fromEmail = "user@localhost";
    var body = Request["requestDate"];
    var requestorName = Request["requestorName"];
    var subject = requestorName;//( "IT Account Request for "+ @requestorName +" ." );

    try {
        WebMail.SmtpServer = "localhost";
        WebMail.SmtpPort = 25;
        WebMail.EnableSsl = false;
        WebMail.UserName = "user@localhost";
        WebMail.Password = "smtppassword";
        WebMail.From = fromEmail;
        WebMail.Send(to: "test@localhost",
            subject: subject,
            body: body




        );
    }
    catch (Exception ex ) {
        errorMessage = ex.Message;
    }
}
}
<div>Error: @errorMessage </div>
<form action="" method="post" enctype="text/plain">
    <table id="requestor_information_form">
        <tr>
            <td class="top"><img src="images\logo.png"></img></td>
            <td class="top">Account Request</td>
            <td class="top" colspan ="2"></td>
        </tr>

        <tr>
            <td class="tableHeading" colspan="4">Requestor Information</td>
        </tr>

        <tr>
            <td>Requestor:</td>
            <td><input type="text" name="requestorName"></td>

            <td>Request Date:</td>
            <td><input type="date" name="requestDate"></td>
        </tr>

        <tr>
            <td>Requestor Phone:</td>
            <td><input type="text" name="requestorPhoneNum"></td>
                    </tr>
  </form>

您需要删除enctype从形式,或者将其设置为application/x-www-form-urlencoded这是默认的。

The point of MVC really is separation of concerns. MVC的重点实际上是关注点分离。 So having your SMTP code right in your view is definitely bad practice here. 因此,在您的视图中正确使用SMTP代码绝对不是一个好习惯。

What you really want to do is create a ViewModel which will represent your form fields being submitted: 您真正想要做的是创建一个ViewModel,它将代表您要提交的表单字段:

public class EmailViewModel 
{
    public string RequestorName { get; set; }
    public string RequestorPhoneNum { get; set; }
    public DateTime RequestorDate { get; set; }
}

This way, we can manage form data in an object oriented way. 这样,我们可以以面向对象的方式管理表单数据。 Now your Controller should handle data passed to/from your views. 现在,您的控制器应处理从视图传递到/从视图传递的数据。 So you'd want to build your controller something similar to: 因此,您需要构建类似于以下内容的控制器:

public class EmailController : Controller
{
    public ActionResult Email() 
    {
        var viewModel = new EmailViewModel();

        // Pass an empty view model to your view
        return View(viewModel);
    }

    // This controller action will be hit when you submit your form
    // and the default MVC model binder will create your viewModel with
    // the appropriate values submitted from your form.
    [HttpPost]
    public ActionResult Email(EmailViewModel viewModel)
    {
        // Make sure the data coming server-side is valid
        // You can use validation attributes like [Required] in your View Model
        if(ModelState.IsValid)
        {
            try 
            {
                WebMail.SmtpServer = "localhost";
                WebMail.SmtpPort = 25;
                WebMail.EnableSsl = false;
                WebMail.UserName = "user@localhost";
                WebMail.Password = "smtppassword";
                WebMail.From = "user@localhost";;
                WebMail.Send(
                    "test@localhost", 
                    viewModel.RequestorName, // RequestorName entered from form
                    viewModel.RequestorDate.ToString() // RequestorDate entered from form
                );
            }
            catch (Exception ex ) 
            {
                errorMessage = ex.Message;
            }
        }

        return View(viewModel);
    }
}

and now you'd want to strongly type your View, so that it can build your form fields for you: 现在您要强烈键入View,以便它可以为您建立表单字段:

@model EmailViewModel

@using(Html.BeginForm())
{
<table id="requestor_information_form">
    <tr>
        <td class="top"><img src="images\logo.png"></img></td>
        <td class="top">Account Request</td>
        <td class="top" colspan ="2"></td>
    </tr>

    <tr>
        <td class="tableHeading" colspan="4">Requestor Information</td>
    </tr>

    <tr>
        <td>@Html.LabelFor(x => x.RequestorName)</td>
        <td>@Html.TextBoxFor(x => x.RequestorName)</td>

        <td>@Html.LabelFor(x => x.RequestorDate</td>
        <td>@Html.TextBoxFor(x => x.RequestorDate)</td>
    </tr>

    <tr>
        <td>@Html.LabelFor(x => x.RequestorPhoneNum)</td>
        <td>@Html.TextBoxFor(x => x.RequestorPhoneNum)</td>
    </tr>

    <tr>
        <td></td>
        <td><input type="submit" value="Send Email" /></td>
    </tr>
</table>
}

Again, the point of MVC is separation of concerns. 同样,MVC的重点是关注点分离。 So, your View's should strictly be the user interface with absolutely no business logic within them. 因此,您的View应该严格是用户界面,其中绝对没有业务逻辑。 Let the controller handle what to do with the data, and always use ViewModels to hold data being transferred to/from your views. 让控制器处理与数据相关的操作,并始终使用ViewModels来保存与视图之间传输的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM