我对MVC很新,对此有点感到难过。
我昨晚在我的Umbraco MVC 4应用程序(6.1.6)中实现了ReCaptcha。 ReCaptcha工作,控制器获得正确的验证码有效状态,一切都很好。问题是当验证码不正确时,我设置ModelState.AddModelError(字符串,字符串)并返回模型以显示验证码输入不正确,但视图从不触发Html.ValidateMessage("验证码) -error")显示在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>
答案 0 :(得分:2)
我认为问题是因为ReCaptcha不是一个简单的Web控件。我认为你可以轻松地解决问题,改变你的&#34;验证码结果&#34; div具有新的类或样式属性。
<div id="captcha-result" class="someClassWithRedBigFont" >
@Html.ValidationMessage("captcha-error")
</div>
如果您使用jQuery,可以查看jQuery Validate plugin。