首页 > 解决方案 > 单击 Razor Pages Asp 发送电子邮件和重定向

问题描述

正如标题所暗示的那样,我很难通过单击发布和重定向

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

                                    <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);

                }
            }

标签: jqueryasp.net.netasp.net-corerazor-pages

解决方案


如果你想在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");
                }
            )
        });

结果: 在此处输入图像描述


推荐阅读