【发布时间】:2019-02-15 08:28:07
【问题描述】:
我是 dot net 的新手。我尝试在 recaptha 未填充但 ModelState.AddModelError 未显示错误时显示错误消息。所以我已经尝试过这种方式,但错误仍然不会显示在我的视图中
模型我的模型文件
public class ContactFormViewModel{
[Required(ErrorMessage ="Required")]
public string FullName { get; set; }
[Required(ErrorMessage = "Required")]
public string Email { get; set; }
[Required(ErrorMessage = "Required")]
public string Subject { get; set; }
public string Message { get; set; }
public string ErrorMessageCaptcha { get; set; }
public string FeedbackField { get; set; }
public string SubmittedFromUrl { get; set; }
}
控制器我的控制器文件
public ActionResult SubmitFormAsync(ContactFormViewModel submittedForm)
{
RecaptchaVerificationHelper recaptchaHelper = this.GetRecaptchaVerificationHelper();
if (string.IsNullOrEmpty(recaptchaHelper.Response))
{
// ModelState.AddModelError(string.Empty, "Please complete the reCAPTCHA");
// ModelState.AddModelError("reCAPTCHA", "The reCAPTCHA is incorrect");
submittedForm.ErrorMessageCaptcha = "Email not found or matched";
// return CurrentUmbracoPage();
return PartialView(submittedForm);
// return View();
}
else
{
RecaptchaVerificationResult recaptchaResult = recaptchaHelper.VerifyRecaptchaResponse();
if (recaptchaResult != RecaptchaVerificationResult.Success)
{
// ModelState.AddModelError("reCAPTCHA", "Please complete the reCAPTCHA");
//ModelState.AddModelError(string.Empty, "The reCAPTCHA is incorrect");
submittedForm.ErrorMessageCaptcha = "Email not found or matched1";
// return CurrentUmbracoPage();
return PartialView(submittedForm);
// return View();
}
}
//FeedbackField is Honeypot captcha
if (!ModelState.IsValid || !string.IsNullOrEmpty(submittedForm.FeedbackField))
{}
查看我的查看文件
<div class="form-group">
<input type="hidden" class="hidden" name="PageId" value="@currentPage.Id" />
@Html.Recaptcha(theme: Recaptcha.Web.RecaptchaTheme.Clean)
<p class="error-message">@Model.ErrorMessageCaptcha</p>
@*@if (ViewData.ModelState.IsValid)
{
@Html.ValidationSummary()
}*@
@Html.Label("error message", new { style = "display:none", id = "recaptchaErrorMessage" })
<button type="submit" id="btn-form-submit" class="btn-secondary pull-right">@currentPage.GetPropertyValue("buttonText")</button>
</div>
【问题讨论】:
标签: c# asp.net-mvc forms umbraco umbraco7