我已经安装了captcha plugin并按照安装说明进行操作.验证码在页面上呈现完美,但在提交表单时,无论我输入什么,ModelState.IsValid始终为true.显然,如果验证码不能验证那么它对我来说并不好.
这是我的控制器:
[HttpPost]
[CaptchaValidation("CaptchaCode", "SampleCaptcha", "Incorrect CAPTCHA code!")]
public ActionResult Register(AccountModel model)
{
if (!ModelState.IsValid)
{
// TODO: Captcha validation failed, show error message
}
else
{
// TODO: Captcha validation passed, proceed with protected action
}
return View();
}
这是我的看法:
<link href="@BotDetect.Web.CaptchaUrls.Absolute.LayoutStyleSheetUrl" rel="stylesheet" type="text/css" />
<form class="form-horizointal" action="@Url.Action("Register", "Account")" method="POST">
<div class="form-group">
@Html.LabelFor(m => m.FirstName, new { @class = "col-sm-2 control-label" })
@Html.TextBoxFor(m => m.FirstName, new { placeholder = "First Name"})
@Html.LabelFor(m => m.LastName, new { @class = "control-label", placeholder = "Last Name" })
@Html.TextBoxFor(m => m.LastName, new { placeholder = "Last Name" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.Email, new {@class = "col-sm-2 control-label", placeholder = "Email"})
@Html.TextBoxFor(m => m.Email, new {placeholder = "Email"})
</div>
<div class="form-group">
@{ MvcCaptcha sampleCaptcha = new MvcCaptcha("SampleCaptcha"); }
@Html.Captcha(sampleCaptcha)
@Html.TextBox("CaptchaCode")
</div>
<div class="form-group">
captcha goes here
</div>
<div class="form-group">
<input type="submit" class="btn btn-default" value="Register"/>
</div>
</form>
有谁知道为什么这对我不起作用?谢谢你的帮助!
解决方法:
我不确定这是不是同样的问题.我在模态弹出窗口中有一个botdetect验证码.它工作正常,直到通过第一次验证确定,之后它总是如此,所以我添加了一个samplecaptcha.Reset(),当它无效时,Model.IsValid再次为假.
@{
Layout = null;
MvcCaptcha sampleCaptcha = new MvcCaptcha("SampleCaptcha");
sampleCaptcha.Reset();
}
[等等]