Copied to clipboard

Flag this post as spam?

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


  • pronto 61 posts 172 karma points
    Sep 21, 2015 @ 13:40
    pronto
    0

    Retrieving Merchello Basket

    Hi,

    I'm trying to call the current Merchello basket from an Umbraco Partial View Macro file, so that I can gather info such as current number of items in the current basket etc.. but I can't seem to get it to work with the example code.

    I've copied the following code from the Basket.cstml script in the Bazaar example but get this error: Exception: System.InvalidOperationException: The model item passed into the dictionary is of type 'Umbraco.Web.Models.PartialViewMacroModel', but this dictionary requires a model item of type 'Merchello.Bazaar.Models.ViewModels.BasketModel'.

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<BasketModel>
    @using Merchello.Bazaar
    @using Merchello.Bazaar.Models.ViewModels
    
    @using System.Web.Mvc.Html
    @using Merchello.Bazaar.Models
    @using Merchello.Core.Models
    @using Merchello.Web
    @using Umbraco.Web
    
    <a href="@Model.BasketPage.Url">Basket (@Model.CurrentCustomer.Basket().TotalItemCount)</a>
    

    It's strange because the script runs fine when rendered on the basket page itself, but I can't seem to use that little snippet anywhere else! Can anyone offer any help?

    Kind Regards

  • Tobias Lopez 64 posts 210 karma points
    Sep 21, 2015 @ 14:44
    Tobias Lopez
    0

    How are you calling the partial macro view?

    @Html.Partial("PARTIALVIEW", YOURBASKETMODEL)
    
  • pronto 61 posts 172 karma points
    Sep 21, 2015 @ 14:53
    pronto
    0

    Hey Tobias,

    Thanks for looking, I'm rendering my macro views like so:

    @Umbraco.RenderMacro("MyPartialView")
    

    Maybe I'm doing something wrong?

    Kind Regards

  • Tobias Lopez 64 posts 210 karma points
    Sep 21, 2015 @ 15:07
    Tobias Lopez
    0

    Yeh you have to pass in your model.

    @Umbraco.RenderMacro("MyPartialView", YOURBASKETMODEL)
    
  • pronto 61 posts 172 karma points
    Sep 21, 2015 @ 16:21
    pronto
    0

    Thanks Tobias, that looks like it's probably the answer, but I'm still having some trouble passing the correct model through unfortunately!

    My Header script looks like this:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
        @Umbraco.RenderMacro("Basket", "BasketModel")
    }
    

    And my Basket.cshtml partial looks like this:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<Merchello.Bazaar.Models.ViewModels.BasketModel>
    @using System.Web.Mvc.Html;
    @using Merchello.Bazaar.Models;
    @using Merchello.Core.Models;
    @using Merchello.Web;
    @using Umbraco.Web;
    
    @{
        <a href="@Model.BasketPage.Url">Basket (@Model.CurrentCustomer.Basket().TotalItemCount)</a>
    }
    

    This gives the error "The model item passed into the dictionary is of type 'Umbraco.Web.Models.PartialViewMacroModel', but this dictionary requires a model item of type 'Merchello.Bazaar.Models.ViewModels.BasketModel'."

    Can anyone spot what I've done wrong?

    Kind Regards

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Sep 21, 2015 @ 16:48
    Rusty Swayne
    0

    @pronto - You should be using

     @Html.Partial(
    

    rather than

     @Umbraco.RenderMacro
    
  • pronto 61 posts 172 karma points
    Sep 21, 2015 @ 20:24
    pronto
    0

    Hey Rusty,

    Thanks for the reply, I've tried your suggestion but the same thing happens!

    Is there a way to retrieve the basket + contents from any partial view macro? I installed the Bazaar store on a pre-existing dev site so I've been having difficulty trying to call Merchello on my non-Merchello pages. Any advice?

    Kind Regards

  • Tobias Lopez 64 posts 210 karma points
    Sep 22, 2015 @ 05:48
    Tobias Lopez
    0

    @pronto could you try something like this?

    var basketUmbracoNode = Umbraco.TypedContent(BASKETNODEID);
    var basketModel = new BasketModel(basketUmbracoNode);
    @Html.Partial("BASKETVIEW", basketModel)
    

    I didn't tested the code... but I think it should look kinda like this.

  • pronto 61 posts 172 karma points
    Sep 22, 2015 @ 09:06
    pronto
    0

    Hey Tobias,

    I managed to pass the correct model through to my basket partial thanks to your code! But for some reason when I call the @Model.CurrentCustomer.Basket() method, I get the following error:

    System.ArgumentNullException: Value cannot be null. Parameter name: customer at Umbraco.Core.Mandate.That[TException](Boolean condition, Func`1 defer) at Merchello.Web.Workflow.Basket.GetBasket(IMerchelloContext merchelloContext, ICustomerBase customer) in c:\Working Repositories\GitHub\Merchello\src\Merchello.Web\Workflow\Basket.cs:line 107
    

    I feel like I'm close but it's driving me crazy! Can anyone offer some advice?

    Kind Regards

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Sep 22, 2015 @ 14:35
    Rusty Swayne
    0

    @pronto

    My guess is you've included this code in a view where the CustomerContext is null. If you swap out

     @inherits Umbraco.Web.Mvc.UmbracoViewPage<
    

    with

     @inherits Merchello.Web.Mvc.MerchelloViewPage<
    

    I bet it kicks.

    MerchelloViewPage

      var customer = CurrentCustomer;
      var basket = CurrentCustomer.Basket();
    
  • pronto 61 posts 172 karma points
    Sep 22, 2015 @ 15:22
    pronto
    0

    Hey Rusty,

    Thank you so much man, that's exactly what I needed. Keep up the good work!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies