Recaptcha不适用于ajax和部分视图

时间:2014-11-25 16:03:08

标签: asp.net-mvc-4 recaptcha mvcrecaptcha

我在我的MVC4网络应用中使用了Recaptcha。当它嵌入到表单中时它工作正常但是当我将@ Html.Raw(Html.GenerateCaptchaHelper())移动到局部视图并尝试通过ajax请求调用此局部视图时,它不起作用!< / p>

分机代码:

public static string GenerateCaptchaHelper(this HtmlHelper helper)
    {
        var captchaControl = new Recaptcha.RecaptchaControl
        {
            ID = "recaptcha",
            Theme = "clean",
            PublicKey = ************,
            PrivateKey = **********************,
            Language = "En"
        };
        var htmlWriter = new HtmlTextWriter(new StringWriter());
        captchaControl.RenderControl(htmlWriter);
        return htmlWriter.InnerWriter.ToString();
    }

我的局部视图有如下代码:        <p>@Html.Raw(Html.GenerateCaptchaHelper())</p> 在我的控制器里面

public PartialViewResult Captcha()
    {
        return PartialView();
    }

在我的主视图中:

 @using (Html.BeginForm("Login", "Account", new { returnUrl = ViewData["ReturnUrl"] }, FormMethod.Post,null))
        {
            @Html.AntiForgeryToken()
            <form role="form">
                <div class="form-group">
                    <label>@Html.LabelFor(m => m.Email)</label><br />
                    @Html.TextBoxFor(m => m.Email, null, new { @class = "form-control", autocomplete = "off" })
                </div>
                <div class="form-group">
                    <label>@Html.LabelFor(m => m.Password)</label><br />
                    @Html.PasswordFor(m => m.Password, new { @class = "form-control", autocomplete = "off" })
                </div>
                <div id="captchaView" class="form-group">
                </div>
                <button type="submit">Login</button>
                    </div>
                </div>
            </form>
        }

和javascript是:

         $.ajax({
                url: '/Account/Captcha',
                type: 'GET',
                success: function(data) {
                    $('#captchaView').html(data);
                }
            });

你能帮我弄清楚原因吗?

0 个答案:

没有答案