我在我的 MVC4 Web 应用程序中使用 Recaptcha。它在嵌入表单时工作正常,但是当我将 @Html.Raw(Html.GenerateCaptchaHelper()) 移动到部分视图并尝试通过 ajax 请求调用此部分视图时,它不起作用!
扩展代码:
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);
}
});
你能帮我弄清楚为什么吗?