MvcCaptcha 示例 — Ajax加载
注:本页缓存10分钟
View:
@using Webdiyer.WebControls.Mvc @using (Html.BeginForm()) { <div>@ViewData["Message"]</div> @Html.ValidationSummary() @Html.MvcCaptcha(new MvcCaptchaOptions { DelayLoad = true, ValidationInputBoxId = "_mvcCaptchaText", CaptchaImageContainerId = "captchaImage" }) <span id="captchaImage"></span> <br /> <div>请点击右边的文本框,输入上边图片中的四个字母或数字:</div> <input type="text" name="_mvcCaptchaText" id="_mvcCaptchaText" /> <input type="submit" value="提交" /> }
Controller:
[OutputCache(Duration = 600, VaryByParam = "none", VaryByHeader = "none")] public ActionResult Ajax() { return View(); } [HttpPost, ValidateMvcCaptcha()] public ActionResult Ajax(FormCollection values) { if (ModelState.IsValid) { //在这里执行表单提交成功后的操作 ViewData["Message"] = "<span class="\"success\">验证通过,表单提交成功!</span>"; } else ViewData["Message"] = "<span class="\"error\">验证未通过,表单提交失败!</span>"; return View(); }
该示例演示如何使用MvcCaptcha验证码控件的Ajax加载验证码图片功能。