我是MVC的新手,在这一点上有点困惑。
我昨晚在我的Umbraco MVC4应用程序(6.1.6)中实现了ReCaptcha。ReCaptcha正常工作,控制器获得正确的验证码有效状态,一切正常。问题是当验证码不正确时,我设置ModelState.AddModelError(字符串,字符串)并返回模型,以便显示输入的验证码不正确,但视图从不触发Html.ValidateMessage(“验证码-错误”)来显示ModelState中更新的实际错误消息。
如有任何帮助或建议,我们将不胜感激
控制器:
[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
}
}查看:
@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>发布于 2014-08-01 17:43:11
我认为问题是因为ReCaptcha不是一个简单的web控件。我认为用新的class或style属性更改"captcha-result“div可以很容易地解决问题。
<div id="captcha-result" class="someClassWithRedBigFont" >
@Html.ValidationMessage("captcha-error")
</div>如果你使用jQuery,你可以查看jQuery Validate plugin。
https://stackoverflow.com/questions/23298268
复制相似问题