Reputation: 2215
I am trying to allow google reCaptcha in my registration process and I have gotten it to display. However when I try to run through my registration to test it I get the following error:
My understanding of the error is that captchaValid is not in the RegisterViewModel
, this makes no sense since here it is defined in my RegisterViewModel:
[Required]
public bool? captchaValid { get; set; }
and here in my controller I have:
public async Task<ActionResult> Register(RegisterViewModel model, string message, bool captchaValid)
What am I missing here?
Upvotes: 0
Views: 220
Reputation: 37060
You can either:
captchaValid
property to a bool
Register
method to take a bool?
Register
method with that value:.
bool defaultCaptchaValue = false;
Register(model, message, captchaValid.GetValueOrDefault(defaultCaptchaValue));
Upvotes: 2
Reputation: 218852
Your http post action method has a parameter called captchaValid
of bool
type. So when you submit the form, There should be one form field (in the request body) or query string key(if the form submit is of GET type) item matching this name for routing to work. The route engine looks for the request data/querystring and based on the parameters, it redirects to the corresponding
But your view model already has a property called captchaValid
. So remove that from your action method parameter list and everything should work.(Model binder will map the posted value for this form field to the property of your view model)
public async Task<ActionResult> Register(RegisterViewModel model, string message)
{
// to do : return something
}
Now When you submit the form, it will hit the http post action method and if you are not providing the value for captchaValid
field from your form ,your model validation fails with the message "The captchaValid field is required" (because it is marked as a required field)
Upvotes: 1