Return CurrentUmbracoPage() when using Ajax.BeginForm
Hi,
I'm trying to build a blog comment section and to post to my surface controller I followed https://codeshare.co.uk/blog/how-to-change-a-form-in-mvc-to-submit-with-ajax/. This works great and I can successfully return a success\failure partial based on whether it went well or not. There is a comment form per comment left and a comment form looks like so and there can be x amount of forms per page (it all depends on how many comments)
However, in addition to some client-side validation on the google ReCaptcha i was writing the following code to validate the response server-side
var captchaResponse = Captcha.ValidateCaptcha(Request["g-recaptcha-response"]);
if (captchaResponse.ErrorCodes != null)
{
ModelState.AddModelError("Captcha", "Please confirm you're not a robot");
return CurrentUmbracoPage();
}
The use of 'return CurrentUmbracoPage()' is causing the following error. I believe this is due to it not being in the context because I have used ajax to my controller (that could be wrong)
Error","@x":"System.InvalidOperationException: Can only use UmbracoPageResult in the context of an Http POST when using a SurfaceController form\r\n at Umbraco.Web.Mvc.UmbracoPageResult.ValidateRouteData(RouteData routeData)
Can anyone suggest how I can return the current page (and its state) when validation fails. In the event clientside validation is bypassed I need to tell the user their ReCaptcha failed
If you are posting with AJAX, can you really return an umbracopage? Isnt that code inside an API-call not inheriting from Umbraco?
I had much problems using AJAX.beginform when building my comments functionality, but resorted to using jquery ajax instead. Could not get Ajax.beginform to work, the routing seemed totally screwed.
I'm not too sure around the inner workings of the AJAX call that I implemented following Paul's guide but my assumption matches your thoughts, Umbraco probably knows nothing about the request that's been made and therefore can't return the current page because it has no context of it.
I will take a look at jquery AJAX and see if I can change my implementation, i don't suppose you have any examples?
This comment functionality has been the bane of my Christmas ha!
Thanks again for your response, really appreciate it
probably gonna throw out that comments-plugin (because I have other needs), but the basics still remain the same, post comments with AJAX and maybe load subcomments that way also.
Return CurrentUmbracoPage() when using Ajax.BeginForm
Hi,
I'm trying to build a blog comment section and to post to my surface controller I followed https://codeshare.co.uk/blog/how-to-change-a-form-in-mvc-to-submit-with-ajax/. This works great and I can successfully return a success\failure partial based on whether it went well or not. There is a comment form per comment left and a comment form looks like so and there can be x amount of forms per page (it all depends on how many comments)
However, in addition to some client-side validation on the google ReCaptcha i was writing the following code to validate the response server-side
The use of 'return CurrentUmbracoPage()' is causing the following error. I believe this is due to it not being in the context because I have used ajax to my controller (that could be wrong)
Can anyone suggest how I can return the current page (and its state) when validation fails. In the event clientside validation is bypassed I need to tell the user their ReCaptcha failed
Thanks
Paul
If you are posting with AJAX, can you really return an umbracopage? Isnt that code inside an API-call not inheriting from Umbraco?
I had much problems using AJAX.beginform when building my comments functionality, but resorted to using jquery ajax instead. Could not get Ajax.beginform to work, the routing seemed totally screwed.
Hi Rodolphe,
Thanks for providing a response.
I'm not too sure around the inner workings of the AJAX call that I implemented following Paul's guide but my assumption matches your thoughts, Umbraco probably knows nothing about the request that's been made and therefore can't return the current page because it has no context of it.
I will take a look at jquery AJAX and see if I can change my implementation, i don't suppose you have any examples?
This comment functionality has been the bane of my Christmas ha!
Thanks again for your response, really appreciate it
Paul
Hi Paul!
I'm acutually implementing this comments function at the moment. https://viima.github.io/jquery-comments/
It posts everything via AJAX. So I have created some web services to accomodate for the functionality for example http://mysite/umbraco/api/CommentsServiceApi/PostComment to post comments or http://mysite/umbraco/api/CommentsServiceApi/GetComments
probably gonna throw out that comments-plugin (because I have other needs), but the basics still remain the same, post comments with AJAX and maybe load subcomments that way also.
My webservices are standard Web API
Here is some example code för posting with AJAX
Hey Rodolphe,
Apologies for the late response and big thanks for the info!
I'll take a look at everything and see how far i can get :)
Thanks
Paul
is working on a reply...