【发布时间】:2019-08-25 18:21:34
【问题描述】:
更新:
表单回发时将提交其值的控件/字段是什么?
在 ASP.NET MVC 表单中,如果用户双击提交按钮,表单将被提交两次。为了解决这个问题,我实现了here 解释的解决方案。
这是我的解决方案,我在提交表单时禁用了提交按钮,因此无法再次点击:
function preventFromBeingDoubleSubmitted() {
$('form').each(function () {
$(this).submit(function (e) {
if ($("form").valid()) {
// if form is valid, then disable the submit button so it cannot be double clicked (double submitted)
$(this).find(':submit').attr('disabled', 'disabled');
}
});
});
}
$(document).ready(function () {
preventFromBeingDoubleSubmitted();
});
这很好用,但是我在使用 ASP.NET 内置身份代码时遇到了一个非常奇怪的行为。我的登录页面,允许用户使用 Facebook 或 Google 登录(每个按钮都是提交按钮):
这是生成上述登录表单的代码(这是内置的身份模板):
@{
var loginProviders = Context.GetOwinContext().Authentication.GetExternalAuthenticationTypes();
if (loginProviders.Count() > 0)
{
using (Html.BeginForm("ExternalLogin", "Account", new { ReturnUrl = Model.ReturnUrl }))
{
@Html.AntiForgeryToken()
<div class="form-group">
@foreach (AuthenticationDescription p in loginProviders.OrderBy(o => o.Caption))
{
if (string.Equals(p.AuthenticationType, "google", StringComparison.InvariantCultureIgnoreCase))
{
<button type="submit" class="external-login-btn btn-google" id="@p.AuthenticationType" name="provider" value="@p.AuthenticationType" title="Log in using your @p.Caption account">Log in with @p.AuthenticationType</button>
}
if (string.Equals(p.AuthenticationType, "facebook", StringComparison.InvariantCultureIgnoreCase))
{
<button type="submit" class="external-login-btn btn-facebook" id="@p.AuthenticationType" name="provider" value="@p.AuthenticationType" title="Log in using your @p.Caption account">Log in with @p.AuthenticationType</button>
}
}
</div>
}
}
}
上面的代码,应该点击下面的Controller Action(内置标识模板):
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult ExternalLogin(string provider, string returnUrl)
{
return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
}
之后,添加.js代码防止重复提交,外部登录不再起作用。问题是,当用户点击 Login with Facebook 按钮时,提供者名称不再传递给ExternalLogin Action。
如果我删除 preventFromBeingDoubleSubmitted() 函数,提供程序名称将在 ExternalLogin Action 方法中传递,一切正常。
我不明白的是,提供者首先是如何传递给操作方法的?为什么禁用按钮会阻止提供者被传入?
【问题讨论】:
-
看起来您在页面完全加载后立即调用禁用函数。这意味着您将完全禁用它,而不是在第一次单击后禁用它。你应该使用 $(document).on('click', 'form', function () {...});而是
-
我不认为是这种情况,因为禁用是在提交时发生的......另外我可以点击按钮,所以它没有被禁用......它只有在我点击时才会被禁用在按钮上并提交表单。
-
请不要将您的问题编辑成一个完全不同的问题。
-
@Rob:感谢您的评论...我的问题是表单提交时将提交哪些表单控件...两个答案都解释了这个问题。我删除 ASP.NET 身份代码的原因是未来的读者可以从这个问题中受益并快速了解它的要点。我觉得编辑将使未来的读者受益。
-
@HoomanBahreini 您最初的问题是:“我不明白的是,提供者首先是如何传递给操作方法的?为什么禁用按钮会阻止提供者被传递”。这与询问哪些控件提交数据非常不同。
标签: c# jquery asp.net-mvc asp.net-mvc-4 asp.net-identity-2