Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Paul Griffiths 370 posts 1021 karma points
    Jan 03, 2021 @ 10:51
    Paul Griffiths
    0

    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)

    enter image description here

    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

    Thanks

    Paul

  • Rodolphe Toots 34 posts 145 karma points
    Jan 03, 2021 @ 23:21
    Rodolphe Toots
    0

    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.

  • Paul Griffiths 370 posts 1021 karma points
    Jan 04, 2021 @ 07:39
    Paul Griffiths
    0

    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

  • Rodolphe Toots 34 posts 145 karma points
    Jan 07, 2021 @ 12:00
    Rodolphe Toots
    0

    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

  • Rodolphe Toots 34 posts 145 karma points
    Jan 07, 2021 @ 12:05
    Rodolphe Toots
    0

    Here is some example code för posting with AJAX

    postComment: function (commentJSON, success, error) {
                            var ArticleComment = new Object();
                            ArticleComment.ParentId = commentJSON.ParentId;
                            ArticleComment.EntityId = @Model.CurrentPage.Id;
                            ArticleComment.Comment = commentJSON.Comment; 
                            console.log(ArticleComment);
    
                            $.ajax({
                                type: 'post',
                                url: "/umbraco/api/CommentsServiceApi/PostComment",
                                data: ArticleComment,
                                success: function (comment) {
                                    console.log("success callback");
                                    console.log(comment);
                                    success(comment)
                                },
                                error: error
                            });
                        }
    
  • Paul Griffiths 370 posts 1021 karma points
    Jan 09, 2021 @ 15:16
    Paul Griffiths
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft