【问题标题】:Google ReCaptcha not showing error when It is not filled umbraco custom forms未填写 umbraco 自定义表单时,Google ReCaptcha 未显示错误
【发布时间】: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


    【解决方案1】:

    试试这个。验证工作正常。替换这些行

    控制器

    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();
                    }
                }
    

    查看

           <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>
    

    【讨论】:

      【解决方案2】:

      不确定您使用的是什么版本或recaptcha,但我已经在我构建的许多自定义表单上使用了它,如果我是正确的,默认情况下recaptcha 按钮会添加一个额外的输入,当提交表单时会添加它到您的表单数据。所以我在表单上所做的只是检查那个额外的字段并创建我自己的简单错误消息并返回带有错误的当前页面,如下所示:

      var formData = Request.Form;
              var captchaRequest = formData["g-recaptcha-response"];
              if (string.IsNullOrWhiteSpace(captchaRequest) || !ModelState.IsValid)
              {
                  TempData["contactError"] = "Ops... Form Error, There was an error with your details please check them and try again.";
                  return CurrentUmbracoPage();
              }
      

      请记住,即使没有填写 recaptcha,ModelState 也是有效的,在我的情况下,它只是一条捕获所有消息。

      【讨论】:

      • link 我按照他的教程添加了recaptcha,验证工作正常,但AddModelError 或submittedForm.ErrorMessageCaptcha 在视图中没有显示错误。在 ValidationSummary() 中也没有显示 recaptcha 错误,但它显示了其他错误链接所需的名称等
      • 认为您最好的选择是在提交表单后返回页面时检查(调试)您的视图,您错误的两件事未保存到模型中或模型返回时到页面上它没有错误,也不确定你是如何提交表单的,因为如果它通过 javascript 也可能是问题所在,我无法在你的视图周围看到表单。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-26
      相关资源
      最近更新 更多