Copied to clipboard

Flag this post as spam?

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


  • Limberg Michelsen 22 posts 99 karma points
    Jan 15, 2014 @ 17:47
    Limberg Michelsen
    0

    Refresh partial view with Ajax

    I am trying to make a shopping cart that updates using Ajax

    And I am thinking that I should have a partial view with the shopping cart and then reload this partial view using Ajax to update the content of the cart. This seems like an elegant solution to the problem.

    Is this the right approach, or is it better to do something else?

    And if it is, can someone point me to an example of this? I am getting errors when calling f.x.:

            $.ajax({
                url: '@Url.Action("ShopCart")',
                type: 'GET',
                cache: false,
                success: function (data) {
                    alert(data);
                }
            });

    Which is giving me "There is not current PublishedContentRequest, it must be initialized before the RenderRouteHandler executes". It gives the URL: /umbraco/RenderMvc/ShopCart

    And different error when using different URLs. What is the correct url to use to get the partial view? It is located i n /Views/Partials/ShopCart.cshtml . Do I need other changes, like urlRewriting or creating a different controller?

    If I use the URL "/Views/Partials/ShopCart" I get the error: "Cannot configure the request if there is not content assigned"

    Any pointers are welcome - thanks :)

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jan 16, 2014 @ 11:42
    Alex Skrypnyk
    0

    Hi Limberg,

    The best way to do that, is create communication with server via JSON, and don't reload DOM elements with HTML.

    But you have to write some code to bind your cart with server response )) Or Use something like Knockout )) but just for cart it's doesn't make sense.

    THanks,

    Alex

  • Limberg Michelsen 22 posts 99 karma points
    Jan 16, 2014 @ 13:20
    Limberg Michelsen
    0

    Ok, I was thinking of that as an option, but I thought the other solution with a partial view that is updated is cleaner

    Why do you prefer doing this with JSON?

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jan 16, 2014 @ 14:51
    Alex Skrypnyk
    0

    1. JSON is the smallest amount of data needed for communication server - web browser.

    2. When you insert HTML in the page, DOM model is updated. With JSON you don't need that.

    3. With JSON you have ready for USE js object and you can do anything with it.

     

    THanks,

    Alex

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jan 16, 2014 @ 14:53
    Alex Skrypnyk
    0

    But if you do simple web site )) HTML is perfect ) fast and simple solution )) 

     

Please Sign in or register to post replies

Write your reply to:

Draft