1

I've searched for hours and I just do not have an idea of how to solve this.

I have a Winkelwagen Controller:

    [ChildActionOnly]
    public ActionResult WinkelwagenPartial()
    {
        // Get the content of the cart
        OrderregelLijst winkelwagenInhoud = new OrderregelLijst
        {
            Orderregels = (List<Orderregel>)Session["winkelwagen"]
        };

        return PartialView(winkelwagenInhoud);
    }

I load the Partial View in my main layout like this:

                <div id="winkelwagenContainer">
                @{
                    Html.RenderAction("WinkelwagenPartial", "Winkelwagen");
                }
                </div>

This all works fine, the problem now is: How do I refresh my partial view after the content of the shopping cart changes?

I've written the following in jQuery:

        $.ajax({
            url: '/WinkelWagen/WinkelwagenPartial',
            success: function (data) {
                alert(data);
            }
        });

When I add something to the cart or delete something from it, I want the partial view to update. I get the following error though:

The action 'WinkelwagenPartial' is accessible only by a child request.

Which I can understand, since partial views can't be located directly. My question now is though, how can I possibly refresh my partial view?

2
  • is this what you're looking for stackoverflow.com/a/11122358/1113766 Commented Mar 17, 2014 at 22:44
  • If you are usin ChildActionOnly attribute you would not be able to call from your JavaScript.. Commented Mar 18, 2014 at 2:59

2 Answers 2

0

You need to create a different Layout which will only load the contents of the partial, then call load the Partial via AJAX using that Layout.

Sign up to request clarification or add additional context in comments.

Comments

0

In your ajax call, you could call a JsonAction method in the controller that in turn renders the partial view to a string.

    public JsonResult GetWinkleWagonPartial() {
        return Json(RenderPartialViewToString("WinkelwagenPartial", null), JsonRequestBehavior.AllowGet);
    }

    private string RenderPartialViewToString(string viewName, object model)
    {
        if (string.IsNullOrEmpty(viewName))
            viewName = ControllerContext.RouteData.GetRequiredString("action");

        ViewData.Model = model;

        using (var sw = new StringWriter())
        {
            var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
            var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
            viewResult.View.Render(viewContext, sw);

            return sw.GetStringBuilder().ToString();
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.