【问题标题】:Send email and Redirect on click Razor Pages Asp单击 Razor Pages Asp 发送电子邮件和重定向
【发布时间】:2021-08-10 12:58:22
【问题描述】:

正如标题所暗示的那样,我在一次点击时很难发布和重定向

以下是发送电子邮件并重定向到新页面的表单。我知道问题是重定向部分,好像删除重定向,电子邮件发送。

                                    <div class="form-actions no-color">
                                        <p>
                                            From : <input type="text" asp-for="email.From" value="email" /><br />
                                            To : <input type="text" asp-for="email.To" value="@principal.EmailAddress" /><br />
                                            Subject : <input type="text" asp-for="email.Subject" /><br />
                                            Body : <textarea asp-for="email.Body"></textarea><br />
                                            <a asp-page="./Edit" asp-route-id="@item.Id" class="btn btn-md float-end text-white fw-bold mt-3 mb-2 grow" id="emailBtn" style="background-color: #1da1f2">

                                            </a>
                                        </p>
                                    </div>
                                </form>


我在下面使用的 Jquery

 $(function () {
            $("#emailBtn").click(function () {
                $("#emailForm").submit()
                var dataString = $(this).serialize();
                e.preventDefault()
                $.ajax({
                    type: "POST",
                    url: "?handler=SendEmail",
                    data: dataString,
                    success: function () {

                    }
                })
            });
        })

和我的服务器端代码

 public async Task OnPostSendEmail()
            {
            using (var smtp = new System.Net.Mail.SmtpClient("stp"))
                {


                var emailMessage = new MailMessage();
                emailMessage.From = new MailAddress("email");
                emailMessage.To.Add(email.To);
                emailMessage.Subject = email.Subject;
                emailMessage.Body = email.Body;



                await smtp.SendMailAsync(emailMessage);

                }
            }

【问题讨论】:

  • 您调用e.preventDefault(),但您不接受处理函数中的事件参数。另外,当您发送 AJAX 请求(以避免重定向)时,为什么要 submit() #emailForm(我认为这会导致重定向)?

标签: jquery asp.net .net asp.net-core razor-pages


【解决方案1】:

如果你想在ajax之后重定向,你可以使用window.location.href。这是一个演示:

表格:

<form id="emailForm" method="post">
    <div class="form-actions no-color">
        <p>
            From : <input type="text" asp-for="email.From" value="email" /><br />
            To : <input type="text" asp-for="email.To" value="@principal.EmailAddress" /><br />
            Subject : <input type="text" asp-for="email.Subject" /><br />
            Body : <textarea asp-for="email.Body"></textarea><br />
            <a asp-page="./Edit" asp-route-id="@item.Id" class="btn btn-md float-end text-white fw-bold mt-3 mb-2 grow" id="emailBtn" style="background-color: #1da1f2">

            </a>
        </p>
    </div>
</form>

js:

$("#emailBtn").click(function () {
            var dataString = $("#emailForm").serialize();
            $.ajax({
                type: "POST",
                url: "?handler=SendEmail",
                data: dataString,
            }).done(function () {
                    window.location.href = $("#emailBtn").attr("href");
                }
            )
        });

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    相关资源
    最近更新 更多