使用Postal和jQuery ajax发送电子邮件

时间:2015-08-03 01:04:54

标签: jquery ajax asp.net-mvc-5

我正在处理一个联系表单,我想用ajax发送电子邮件。我正在使用Postal库和MVC 5.当我点击按钮时,我得到一个警告,只是说“未定义”。

以下是视图中的代码:

@model AccessorizeForLess.Models.ContactEmail
@{
    ViewBag.Title = "Contact";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Contact</h2>

@*@using (Html.BeginForm("Send", "Contact", FormMethod.Post))
    {*@
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>
        AccessorizeForLess.net&nbsp;&nbsp;&nbsp;
        <a href="https://www.facebook.com/accessorize5orless" target="_blank" title="Accessorize For Less On Facebook"><img src="~/Content/icon-facebook.png" /></a>
    </h4>
    <div id="sending" style="display:none;"><img src="~/Content/ajax-loader.gif" /></div>
    <div style="color:red">@ViewBag.Message</div>
    <hr />
    @Html.ValidationSummary(true)

    <div class="form-group">
        @Html.LabelFor(model => model.From, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.From, new { @id = "from" })
            @Html.ValidationMessageFor(model => model.From)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.FirstName, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.FirstName)
            @Html.ValidationMessageFor(model => model.FirstName)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.LastName, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.LastName)
            @Html.ValidationMessageFor(model => model.LastName)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.SelectedSubject, "Category", new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.SelectedSubject, new SelectList(Model.Subjects, "Id", "Subject"), "- Please Select -")
            @Html.ValidationMessageFor(model => model.SelectedSubject)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Message, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Message, new { @cols = "25", @rows = "55" })
            @Html.ValidationMessageFor(model => model.Message)
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="button" value="Send" class="btn btn-default" id="SendMessage" />
        </div>
    </div>
</div>


@*}*@
@section Scripts {
    <script type="text/javascript">
        $(function () {
            // Document.ready -> link up remove event handler
            $("#SendMessage").click(function () {
                //first disable the button to prevent double clicks
                $("#SendMessage").attr("disbled", true);
                //now show the loading gif
                $("#sending").css("display", "block");
                // Get our values
                var from = $("#From").val();
                var fName = $("#FirstName").val();
                var lName = $("#LastName").val();
                var subject = $("#SelectedSubject :selected").text();
                var message = $("#Message").val();
                // Perform the ajax post
                $.ajax({
                    url: "@Url.Action("SendAJAX","Contact")",
                    type: "POST",
                    data: { "from": from, "fName": fName, "lName": lName, "subject": subject, "message": message },
                    success: function (data) {
                        $("#SendMessage").attr("disbled", false);
                        $("#sending").css("display", "none");
                    },
                    error: function (data) {
                        $("#SendMessage").attr("disbled", false);
                        $("#sending").css("display", "none");
                        alert(data.message);
                    }
                });                   
            });
        });
    </script>
}

我在ContactController中的 SendAJAX 方法:

public ActionResult SendAJAX(string from, string fName, string lName, string subject, string message)
{
    ContactEmail e = new ContactEmail();
    e.From = from;
    e.FirstName = fName;
    e.LastName = lName;
    e.SelectedSubject = subject;
    e.Message = message;

    e.Send();
    ViewBag.Message = "Your message has been sent";
    return RedirectToAction("Index");
}

我尝试在 SendAJAX 中设置断点,但它从未到达断点(我将断点放在方法的开头)。我检查了Chrome中的JavaScript控制台,它显示了500(内部服务器错误),仅此而已。

1 个答案:

答案 0 :(得分:0)

这似乎是一个权限问题。我想当我升级到Windows 10时,它消灭了我的所有权限,所以我进入IIS并更改了权限,现在发送就好了。