我该如何正确的AJAX POST之后重新呈现的reCAPTCHA在ASP.NET MVC 2 [英] How do I correctly Re-render a Recaptcha in ASP.NET MVC 2 after an AJAX POST

查看:139
本文介绍了我该如何正确的AJAX POST之后重新呈现的reCAPTCHA在ASP.NET MVC 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧...

我已经下载并执行该 验证码实施 作为MVC它使用的ModelState确认验证码控制的有效性。

I've downloaded and implemented this Recaptcha implementation for MVC which uses the ModelState to confirm the validity of the captcha control.

它出色的作品......除了当我开始使用它在一个Ajax表单。

It works brilliantly... except when I start to use it in an AJAX Form.

在简单地说,当一个div重新呈现与AJAX,在和ReCaptcha,它应该包含没有出现,即使相关的<脚本> 在后部分的来源呈现。

In a nutshell, when a div is re-rendered with AJAX, the ReCaptcha that it should contain does not appear, even though the relevant <scripts> are in the source after the partial render.

code以下。

 using (Ajax.BeginForm("CreateComment", "Blog", 
        new AjaxOptions() { HttpMethod = "POST", 
         UpdateTargetId = "CommentAdd", OnComplete="ReloadRecaptcha", 
         OnSuccess = "ShowComment", OnFailure = "ShowComment", 
         OnBegin = "HideComment" }))
{%>
    <fieldset class="comment">
        <legend>Add New Comment</legend>
        <%= Html.ValidationSummary()%>
        <table class="postdetails">
            <tbody>
                <tr>
                    <td rowspan="3" id="blahCaptcha">
                        <%= Html.CreateRecaptcha("recaptcha", "blackglass") %>
                    </td>
                    .... Remainder of Form Omitted for Brevity

我已经证实了这一形式是完美的功能时验证码控制并不present和JavaScript调用的 AjaxOptions 都工作正常。

现在的问题是,如果的ModelState 是无效的验证码或其他验证的结果,那么的ActionResult 返回查看,以重新出现。形式。

The problem is that if the ModelState is Invalid as a result of the Recaptcha or some other validation, then the ActionResult returns the View to reshow the form.

    [RecaptchaFilter(IgnoreOnMobile = true)]
    [HttpPost]
    public ActionResult CreateComment(Comment c)
    {
        if (ModelState.IsValid)
        {
            try
            {
                //Code to insert Comment To DB
                return Content("Thank You");
            }
            catch
            {
                ModelState.AddRuleViolations(c.GetRuleViolations());
            }
        }
        else
        {
            ModelState.AddRuleViolations(c.GetRuleViolations());
        }
        return View("CreateComment", c);
   }

当它是无效的,形式的帖子后面,由于某种原因,和ReCaptcha控制不重新呈现。我检查了源和&LT;脚本&GT; &放大器; &LT; NOSCRIPT&GT; 块是present在HTML所以下面的HTML辅助功能显然是工作

When it's InValid and the form posts back, for some reason the ReCaptcha Control does not re-render. I've checked the source and the <script> & <noscript> blocks are present in the HTML so the HTML Helper function below is obviously working

<%= Html.CreateRecaptcha("recaptcha", "blackglass") %>

我想这已经是与注入到AJAX的DOM不会重新执行的脚本。

I assume this has something to do with scripts injected into the DOM by AJAX are not re-executed.

正如你可以从 HTML 上面的代码段看,我试图添加的onComplete = JavaScript函数重新创建在客户端的验证码,不过虽然没有错误的脚本执行,这是行不通的。的onComplete功能的。

As you can see from the HTML snippet above, I've tried to add an OnComplete= javascript function to re-create the Captcha on the client side, but although the script executes with no errors, it doesn't work. OnComplete Function is.

    function ReloadRecaptcha() {
        Recaptcha.create("my-pub-key", 'blahCaptcha', { 
            //blahCaptcha is the ID of the <td> where the ReCaptcha should go.
            theme: 'blackglass'
            });
    }

任何人都可以阐明这个任何光线?

Can anyone shed any light on this ?

谢谢, 约恩ç

推荐答案

想出了一个解决这个。

我彻底删除了验证码的客户端呈现的HTML帮助。

I completely removed the client side rendering of the Captcha with the HTML Helper.

相反,过滤器保持就位,在服务器端做的ModelState中验证等等...

Instead, the Filter stays in place on the server side for doing the ModelState validation etc...

和客户端呈现在使用和ReCaptcha AJAX API的所有完成 http://recaptcha.net/fastcgi/demo/ajax http://api.recaptcha.net/js/recaptcha_ajax.js

And the client side rendering is all done using the ReCaptcha AJAX Api's from http://recaptcha.net/fastcgi/demo/ajax and http://api.recaptcha.net/js/recaptcha_ajax.js

每次局部发生后,验证码消失,每次它完成时,onComplete脚本重新创建它。

Everytime the partial post occurs, the captcha disappears, everytime it completes, the OnComplete script recreates it.

这篇关于我该如何正确的AJAX POST之后重新呈现的reCAPTCHA在ASP.NET MVC 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆