I am new to dot net.I tried to display the errormessage when recaptha is not filled but ModelState.AddModelError not showing the error. so I have tried in this way but still the error won't show in my view
Model my 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; }
}
Controller my controller file
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))
{}
View my view file
<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>
Try this one. The validation is working fine. Replace these lines
Controller
RecaptchaVerificationHelper recaptchaHelper = this.GetRecaptchaVerificationHelper();
if (string.IsNullOrEmpty(recaptchaHelper.Response))
{
ModelState.AddModelError("reCAPTCHA", "Please complete the reCAPTCHA");
return CurrentUmbracoPage();
}
else
{
RecaptchaVerificationResult recaptchaResult = recaptchaHelper.VerifyRecaptchaResponse();
if (recaptchaResult != RecaptchaVerificationResult.Success)
{
ModelState.AddModelError("reCAPTCHA", "The reCAPTCHA is incorrect");
return CurrentUmbracoPage();
}
}
View
<div class="form-group col-sm-12">
@Html.Recaptcha(theme: Recaptcha.Web.RecaptchaTheme.Clean)
@{
var errors = ViewData.ModelState.Values.SelectMany(v => v.Errors);
}
@if (errors != null && errors.Any())
{
@Html.Label(errors.FirstOrDefault().ErrorMessage, new { id = "recaptchaErrorMessage" })
}
</div>