Я новичок в точечной сети. Я пытался отобразить сообщение об ошибке, когда recaptha не заполнено, но ModelState.AddModelError не показывает ошибку.поэтому я попытался таким образом, но все равно ошибка не будет отображаться в моем представлении
Модель мой Modelfile
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>