单击 Razor 页面时发送电子邮件和重定向 Asp
Send email and Redirect on click 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);
}
}
如果你想在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");
}
)
});
结果:
正如标题所暗示的那样,我无法通过单击进行发布和重定向
下面是发送电子邮件并重定向到新页面的表单。我知道问题出在重定向部分,就像删除重定向一样,电子邮件已发送。
<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);
}
}
如果你想在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");
}
)
});
结果: