我已将 BotDetect Captcha 集成到我的 ASP.NET MVC 表单中,当用户输入错误的 Captcha 值时,我无法显示验证消息。
我需要更改什么才能显示验证消息?
编辑- 我添加了Html.BeginForm
视图的一部分以指示回发将在控制器中执行不同的操作。
源代码 - 查看
@using(Html.BeginForm("Create", "Contact", FormMethod.Post, new { enctype = 'multipart/form-data' }))
{
@{
MvcCaptcha contactCaptcha = new MvcCaptcha("ContactCaptcha");
contactCaptcha.UserInputID = "ContactCaptchaCode";
}
To prevent spam, we utilize a verification code system. Please enter the code as it is shown in the box below.<br />
@Html.Captcha(contactCaptcha)<br />
Code:<br />
@Html.TextBox("ContactCaptchaCode", string.Empty, new { @class = "form- control", @style = "width: 200px" })
<div class="row">
<div class="col-sm-12" style="text-align: right">
<input type="submit" value="Send" class="btn-default" />
@Html.ValidationMessage("ContactCaptchaCode", new { @class = "text-danger" })
</div>
</div>
}
源代码 - 控制器
[HttpPost]
[AllowAnonymous]
[CaptchaValidation("ContactCaptchaCode", "ContactCaptcha", "Please enter the correct CAPTCHA code.")]
public ActionResult Create(Models.Contact formContact)
{
if(ModelState.IsValid)
{
//do stuff here is correct captcha value
}
}