c# - MVC 4 - Captcha.Mvc -
i have captcha control mvc 4 page , cannot show message if input incorrect. i'm used doing things through jquery , on success something, when here lose modelstate.isvalid.
so, when run code captcha control loads fine on page shows 5 letters in image line says 'refresh' , textbox beneath input submit button on index page post controller.
when input wrong refreshes image no message saying wrong, know wrong because controller says modelstate.isvalid false want load new image , display input incorrect.
when input correct refreshes image still no message or anything. want stay there , input correct , disable textbox.
my question: how can described above?
my code below:
controllers/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 not valid")] public actionresult ajaxform(captchaviewmodel model) { if (modelstate.isvalid) { modelstate.clear(); tempdata["message"] = "message: captcha valid."; model.strmessage = "efefwf"; if (request.isajaxrequest()) return partialview("_captcha", model); //return json(model, jsonrequestbehavior.allowget); return view(model); } tempdata["errormessage"] = "error: captcha not valid."; if (request.isajaxrequest()) return partialview("_captcha", model); return view(model); } } }
viewmodels/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; } } }
views/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>
views/shared/_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>
in case still need this:
there 2 options one:
@html.captcha("refresh", "captcha not valid " , 4, "the captcha required", true)
the last true
set bool addvalidationspan
.
another option:
<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>
also <script src="~/scripts/jquery-2.1.4.js"></script>
need loaded before line rendered.
Comments
Post a Comment