MVC 4 - Captcha.Mvc
本文关键字:Mvc Captcha MVC | 更新日期: 2023-09-27 18:06:01
我有一个验证码控制我的MVC 4页,我不能让它显示一个消息,如果输入不正确。我习惯通过jquery来做事情,然后成功地做一些事情,但是当我这样做的时候,我失去了ModelState.IsValid。
所以,当我运行这段代码时,Captcha控件在页面上加载得很好,它在图像中显示了5个字母,一行写着"刷新",下面是一个文本框,用于输入,在我的索引页上有一个提交按钮,可以发布到控制器。
当我输入错误时,它刷新图像,没有消息说任何错误,我知道它是错误的,因为我的控制器说ModelState。IsValid为false,但我想加载一个新图像并显示输入错误。
当我得到正确的输入时,它刷新图像仍然没有消息或任何东西。我想让它停留在那里,说输入是正确的,并禁用文本框。
我的问题是:我怎样才能做到我上面描述的?
我的代码如下:
控制器/HomeController.cs
using System.Web.Mvc;
using CaptchaDemo.MVC4.ViewModels;
using CaptchaMvc;
using CaptchaMvc.Attributes;
using CaptchaMvc.Infrastructure;
namespace CaptchaDemo.MVC4.Controllers
{
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
CaptchaUtils.CaptchaManager.StorageProvider = new CookieStorageProvider();
ViewBag.Title = "Captcha MVC 4 Demo";
return View();
}
public ActionResult _Captcha()
{
CaptchaViewModel model = new CaptchaViewModel();
return View(model);
}
public ActionResult AjaxForm()
{
return View(new CaptchaViewModel());
}
[HttpPost, CaptchaVerify("Captcha is not valid")]
public ActionResult AjaxForm(CaptchaViewModel model)
{
if (ModelState.IsValid)
{
ModelState.Clear();
TempData["Message"] = "Message: captcha is valid.";
model.strMessage = "efefwf";
if (Request.IsAjaxRequest())
return PartialView("_Captcha", model);
//return Json(model, JsonRequestBehavior.AllowGet);
return View(model);
}
TempData["ErrorMessage"] = "Error: captcha is not valid.";
if (Request.IsAjaxRequest())
return PartialView("_Captcha", model);
return View(model);
}
}
}
视图模型/CaptchaViewModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CaptchaDemo.MVC4.ViewModels
{
public class CaptchaViewModel
{
public string strMessage { get; set; }
}
}
视图/Home/Index.cshtml
@using (Html.BeginForm("AjaxForm", "Home", FormMethod.Post, new { @id = "AjaxCaptchaForm", @class = "ajax" }))
{
<div id="update">@Html.Partial("_Captcha")</div>
<input type="submit" />
}
<script type="text/javascript">
$(document).ready(function () {
$('#AjaxCaptchaForm').submit(function () {
$.post($(this).attr("action"), $(this).serialize(), function (results) {
$("#update").html(results);
});
return false;
});
});
</script>
视图/共享/_Captcha.cshtml
@using CaptchaMvc.HtmlHelpers
@model CaptchaDemo.MVC4.ViewModels.CaptchaViewModel
@Html.ValidationSummary(true)
@Html.ValidationMessageFor(model => model.strMessage)
@Html.Captcha(5)
<span>@Model.strMessage</span>
如果有人仍然需要帮助:
有两个选项:
@Html.Captcha("Refresh", "Captcha is not valid "
, 4, "The Captcha is required", true)
最后一个true
设置bool addValidationSpan
。
另一种选择:
<span class="field-validation-valid text-danger" data-valmsg-for="CaptchaInputText" data-valmsg-replace="true" id="vali_CaptchaInputText"></span>
<span class="field-validation-valid text-danger" data-valmsg-for="CaptchaDeText" data-valmsg-replace="true" id="vali_CaptchaDeText"></span>
<script src="~/Scripts/jquery-2.1.4.js"></script>
也需要在这条线被渲染之前加载。
@Html.Captcha(5)
呈现id="CaptchaInputText"
的输入。要显示本地警告消息,您需要在模型后面添加CaptchaInputText
属性并添加
@Html.ValidationMessageFor(m => m.CaptchaInputText, String.Empty, new { @class = "validation-error", @style = "color:red;" })