首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未触发ReCaptcha MVC剃刀验证消息

未触发ReCaptcha MVC剃刀验证消息
EN

Stack Overflow用户
提问于 2014-04-26 00:20:17
回答 1查看 2.7K关注 0票数 1

我是MVC的新手,在这一点上有点困惑。

我昨晚在我的Umbraco MVC4应用程序(6.1.6)中实现了ReCaptcha。ReCaptcha正常工作,控制器获得正确的验证码有效状态,一切正常。问题是当验证码不正确时,我设置ModelState.AddModelError(字符串,字符串)并返回模型,以便显示输入的验证码不正确,但视图从不触发Html.ValidateMessage(“验证码-错误”)来显示ModelState中更新的实际错误消息。

如有任何帮助或建议,我们将不胜感激

控制器:

代码语言:javascript
复制
[HttpPost]
[RecaptchaControlMvc.CaptchaValidator]
public ActionResult SubmitContactFormAction(Models.ContactFormModel model, bool captchaValid, string captchaErrorMessage)
{
    if (!captchaValid)
    {
        ModelState.AddModelError("captcha-error", captchaErrorMessage);
        return View(model);
    }
    else
    {
        // Test ModelState and do stuff
    }
}

查看:

代码语言:javascript
复制
@model WebStaging.Models.ContactFormModel
@using Recaptcha
@using (Ajax.BeginForm("SubmitContactFormAction", "ContactForm", null, new AjaxOptions { HttpMethod = "POST" }, new { @id = "frmContactForm" }))
{
    @Html.ValidationSummary(true)
    <div>
    Fields marked with * are required<br/><br />
    <div class="clear pad-5">
        <div class="label-column">
        @Html.LabelFor(model => model.Name) * 
        </div>
        <div class="input-column">
        @Html.TextBoxFor(model => model.Name, new { @class = "input-242" })<br />
        @Html.ValidationMessageFor(model => model.Name)
        </div>
    </div>
    <div class="clear pad-5">
        <div class="label-column">
        @Html.LabelFor(model => model.Email) * 
        </div>
        <div class="input-column">
        @Html.TextBoxFor(model => model.Email, new { @class = "input-242" })
        </div>
    </div>
    <div class="clear pad-5">
        <div class="label-column">
        @Html.LabelFor(model => model.Phone) 
        </div>
        <div class="input-column">
        @Html.TextBoxFor(model => model.Phone, new { @class = "input-242" })
        </div>
    </div>
    <div class="clear pad-5">
        <div class="label-column">
        @Html.LabelFor(model => model.Subject) * 
        </div>
        <div class="input-column">
        @Html.TextBoxFor(model => model.Subject, new { @class = "input-242" })
        </div>
    </div>
    <div class="clear pad-5">
        <div class="label-column">
        @Html.LabelFor(model => model.Message) * 
        </div>
        <div class="input-column">
        @Html.TextAreaFor(model => model.Message, new { @class = "input-textarea" })
        </div>
    </div>
    <br />
    <div id="captcha-panel" class="clear" style="text-align: right; float: right; padding-top: 5px;">
        @Html.Raw(Html.GenerateCaptcha("captcha", "white"))       
    </div>
    <p>
    <div id="captcha-result" class="clear">
        @Html.ValidationMessage("captcha-error")
    </div>
    <div class="clear pad-5" style="float: right; text-align: center;">
        <button type="submit">Send</button><br />  
        <div id="submit-status" style="color: Green;"></div>
    </div>
</div>
EN

回答 1

Stack Overflow用户

发布于 2014-08-01 17:43:11

我认为问题是因为ReCaptcha不是一个简单的web控件。我认为用新的class或style属性更改"captcha-result“div可以很容易地解决问题。

代码语言:javascript
复制
 <div id="captcha-result" class="someClassWithRedBigFont" >
    @Html.ValidationMessage("captcha-error")
</div>

如果你使用jQuery,你可以查看jQuery Validate plugin

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23298268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档