Copied to clipboard

Flag this post as spam?

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


  • Jan A 59 posts 264 karma points
    Feb 02, 2024 @ 11:43
    Jan A
    0

    BeginUmbracoForm inside Component rendered from ajax call

    I have a Component that renders a form When the page is renders for the first time i simply use the Component.InvokeAsync

    @(await Component.InvokeAsync("Cart"))
    

    Inside of this I have a BeginUmbracoForm that calls a surface controller

    @using (Html.BeginUmbracoForm<CartController>(nameof(CartController.SaveCart)))
    

    If I post this it will call the same url with the token, and Umbraco routes it to the surface controller.

    Then I have an ajax call on the page to load another cart by calling the CartController

    url: /umbraco/cart/loadcart

    This will change the cart and then render the Component and return it to the javascript that will update some of the html without reloading the page.

    The problem is that when the component is rendered this time, the request url is /umbraco/cart/loadcart and the BeginUmbracoForm will therefore post the surface call to this url. I realise why this is (since the request url is this), but I need this to not do it. Is there any workaround for this? Should I add a div with id "formcontent" inside the BeginUmbracoForm and only update the form content by ajax, keeping the original html form tag?

  • Huw Reddick 1749 posts 6114 karma points MVP c-trib
    Feb 02, 2024 @ 12:54
    Huw Reddick
    0

    You can just hardcode the action parameter

  • Jan A 59 posts 264 karma points
    Feb 07, 2024 @ 12:07
    Jan A
    0

    Ok, and thanks for input.

    I think I had the problem wrong. The actual problem is that the SaveCart method is returning Ok() in the end or CurrentUmbracoPage()

    Even if I hardcode the Html.BeginUmbracoForm

    On the ajaxload the action it will therefore be /umbraco/cart/loadcart

    The Save will be called ok, but the CurrentUmbracoPage() will return /umbraco/cart/loadcart

Please Sign in or register to post replies

Write your reply to:

Draft