0

I have integrated the BotDetect Captcha in to my ASP.NET MVC form and I can not get the validation message to display when the user enters the incorrect Captcha value.

What do I need to change to get the validation message to display?

Edit - I added the Html.BeginForm portion of the view to indicate that the post back is going to a different action in the controller.

Source Code - View

@using(Html.BeginForm("Create", "Contact", FormMethod.Post, new { enctype = 'multipart/form-data' }))
{
    @{ 
        MvcCaptcha contactCaptcha = new MvcCaptcha("ContactCaptcha");
        contactCaptcha.UserInputID = "ContactCaptchaCode";
    }
    To prevent spam, we utilize a verification code system.  Please enter the code as it is shown in the box below.<br />
    @Html.Captcha(contactCaptcha)<br />
    Code:<br />
    @Html.TextBox("ContactCaptchaCode", string.Empty, new { @class = "form-   control", @style = "width: 200px" })                
    <div class="row">
        <div class="col-sm-12" style="text-align: right">
            <input type="submit" value="Send" class="btn-default" />
            @Html.ValidationMessage("ContactCaptchaCode", new { @class = "text-danger" }) 
        </div>
    </div>
}

Source Code - Controller

[HttpPost]
[AllowAnonymous]
[CaptchaValidation("ContactCaptchaCode", "ContactCaptcha", "Please enter the correct CAPTCHA code.")]
public ActionResult Create(Models.Contact formContact)
{
    if(ModelState.IsValid)
    {
        //do stuff here is correct captcha value
    }
}
Michael Kniskern
  • 24,792
  • 68
  • 164
  • 231
  • Are you saying that `ModelState` is always valid even when you enter an incorrect value? –  Mar 11 '17 at 01:00
  • If it is invalid, then all you should need is `else { return View(formContact); }` to return the view and display the error message –  Mar 11 '17 at 01:01
  • @StephenMuecke No, ModelState is valid when they enter a correct CAPTCHA code, when they enter an invalid CAPTCHA code it prevents the form from being processed and `ModelState.IsValid = false` – Michael Kniskern Mar 11 '17 at 01:06
  • And when you return the view if `ModelState` is invalid, are you saying the message is not displayed? –  Mar 11 '17 at 01:08

1 Answers1

1

you have to add !ModelState.IsValid if false then return to the view because in your code if model is invalid do nothing and thats not how validation works. so do something like this

[HttpPost]
[AllowAnonymous]
[CaptchaValidation("ContactCaptchaCode", "ContactCaptcha", "Please enter the correct CAPTCHA code.")]
public ActionResult Create(Models.Contact formContact)
{
    if(!ModelState.IsValid)
  {
      return view(formContact);
  }

    //if valid do your code here
}

now if you place a break point you will see ModelState will have keys and values if any validation fails and when you do return view(formContact); it will return to the view with validation messages and it will match key and display the error message e.g let suppose i have

@Html.ValidationMessage("ContactCaptchaCode");

if validation fails for this property modelstate will have key=ContactCaptchaCode and value=Please enter the correct CAPTCHA code.

Usman
  • 4,615
  • 2
  • 17
  • 33
  • One thing I should mention, I am using the `Html.BeginForm` overload where I explicitly configure the route after form submission. i.e. `("Create", "Contact", FormMethod.Post)`. Would this have an impact on the behavior of the CAPTCHA validation? – Michael Kniskern Mar 22 '17 at 16:03