Copied to clipboard

Flag this post as spam?

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


  • Bendik Engebretsen 105 posts 202 karma points
    Oct 03, 2017 @ 21:15
    Bendik Engebretsen
    0

    Refresh partial from onclick() javascript

    I'm rolling a cookie based shopping cart for my Umbraco site. Now, within the partial view, I have buttons that modify the cart cookie directly through javascript. Hence, after a modification has been done, I need to reload. This works fine if I just call

    document.location.reload();
    

    in the javascript. However, this causes a major flicker of the whole page, ugly:-( So, I'm looking for a way to only reload the partial. I've looked into solutions involving jQuery's load, so something like this:

    $('#thePartialDiv').load('@Url.Action("PartialViewName", "???")');
    

    But, I can't seem to get the Url.Action right. And I don't even know if this would be a viable solution for Umbraco.

    Anyone?

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Oct 03, 2017 @ 23:05
    Alex Skrypnyk
    1

    Hi Bendik

    You can use surface controllers for returning the partial view with data in your Umbraco solution.

    Thanks,

    Alex

  • Marcio Goularte 388 posts 1360 karma points
    Oct 04, 2017 @ 01:54
    Marcio Goularte
    1
    $('#thePartialDiv').load('@Url.Action("MyAction", "MySurface")'); 
    
    public class MySurfaceController : SurfaceController
    {
        [HttpGet]
        public ActionResult MyAction()
        {
           return PartialView("_MyPartial");
        }
    }
    
  • Bendik Engebretsen 105 posts 202 karma points
    Oct 04, 2017 @ 08:19
    Bendik Engebretsen
    100

    Thanks a lot guys! I figured out that I also need to pass a model from the controller action. Hence

    $('#thePartialDiv').load('@Url.Action("GetPartial", "ControllerName", new { NodeId = Model.Id })');
    

    and then in the controller

    [HttpGet]
    public ActionResult GetPartial(int NodeId)
    {
        return PartialView("PartialViewName", Umbraco.TypedContent(NodeId));
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft