Copied to clipboard

Flag this post as spam?

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


  • Petras Surna 90 posts 144 karma points
    Dec 22, 2013 @ 00:37
    Petras Surna
    0

    How do you pass parameters to Partial View Macro Files in 7.01

    I am calling a Partial View Macro

    @Umbraco.RenderMacro("HomePageGallery", new {HomePageGalleryId = "test"})
    

    And I need to get the value of HomePageGalleryId

    If I do this

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using umbraco.MacroEngines
    @Model.MacroParameters.Count
    

    I see 0, which is a worry.

    If I do this, it crashes as there probably isn't any MacroParameters

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using umbraco.MacroEngines
    @Model.MacroParameters["HomePageGalleryId"]
    

    How do I get the HomePageGalleryId passed to the Partial View Macro?

  • David Zweben 268 posts 753 karma points
    Dec 22, 2013 @ 02:31
    David Zweben
    0

    I don't see anything wrong with the code you posted. '.MacroParameters["ParameterAlias"]' is the correct way to do it. What are you doing with the value of the macro parameter? 

    Below is some partial view macro code I'm using myself that gets the value from a macro parameter in 7.0.1 without issue. I would look it over and see if you can find any differences to your code in how it handles the macro parameter, or you can post your code and I'll try to take a look. If you're not already, it may help to set the parameter value to a variable instead of using it directly, and check that the variable is non-null (and represents an valid node or property ID, if it's supposed to) before trying to use it, like below.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    
    @{  
    
        @* Store the PropertyAlias macro parameter in a string *@
        string propAlias = Model.MacroParameters["PropertyAlias"].ToString();
    
        @* Check that the propAlias string is non-empty and is the ID to a node on the current page *@
        if (!String.IsNullOrEmpty(propAlias) && CurrentPage.HasValue(propAlias))
        {
            @* Make a variable to keep track of what array item we're on in the property's JSON data *@
            int counter=0;
    
            @* Loop through the JSON array *@
            foreach (var item in CurrentPage.GetPropertyValue(propAlias))
            {
    
                counter++;
    
                @* Output the current select menu value *@
                var value = (string)item.value;
                @value
    
                @* If we are not on the last array item... *@
                if (counter < CurrentPage.GetPropertyValue(propAlias).Count) 
                {
                    @* Add a comma and a space to the end of our output so far *@
                    @Html.Raw(", ")
                } 
    
                @* If we are on the last array item *@
                else
                {
                    @* Add something different to the end of our output *@
                    @Html.Raw(".")  
                }
    
            }
    
        }
        else
        {
            A valid Property Alias macro parameter must be specified.
        }
    
    
    }    
  • Petras Surna 90 posts 144 karma points
    Dec 22, 2013 @ 03:11
    Petras Surna
    0

    Thanks David but I am still stuck. I changed the calling code to:

    @Umbraco.RenderMacro("HomePageGallery", new {PropertyAlias = "test"})

    and the Partial View Macro to:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using umbraco.MacroEngines
    
    
    @{      
    
      if(Model.MacroParameters.ContainsKey("PropertyAlias"))
      {
        <text>"Its there"</text>
      }
      else
      {
        <text>"Not there"</text>
      }
    
    }
    
    
    @Model.MacroParameters.Count
    

    This returns "Not there" and a Count of 0

    So the parameter is not being passed. I also tried:

    string propAlias = Model.MacroParameters["PropertyAlias"].ToString();
    

    But of course that crashed as there is no such parameter.

    Is there some sort of config option I am missing that's stopping parameters being passed?

    Also, I do have this in umbracoSettings.config

      <templates>
        <!-- To switch the default rendering engine to MVC, change this value from WebForms to Mvc -->
        <defaultRenderingEngine>Mvc</defaultRenderingEngine>
      </templates>
    
  • David Zweben 268 posts 753 karma points
    Dec 22, 2013 @ 03:50
    David Zweben
    101

    Do you have the parameter set up on the Macro itself? You may know this already but it's worth checking: parameters need to be created on the Macro node's Parameters tab before they can be used. All the code that you posted looks fine to me, so I would give that a check. Make sure that you have a parameter specified there, and that the parameter alias matches what you're using in the template. If that looks fine, I would just try deleting your RenderMacro call in the template, and re-inserting it using the 'Insert Macro' button.

    If you don't have any luck with that, I would suggest turning on Debug mode if it's off, and seeing what info you can gleam from using "?UmbracoDebug=true", but I haven't been able to get that to work myself, so I'm afraid I'm of limited help there.

  • Petras Surna 90 posts 144 karma points
    Dec 22, 2013 @ 13:22
    Petras Surna
    0
    Do you have the parameter set up on the Macro itself?
    

    No, I forgot all about that! Now it works.

    Thanks David.

  • David Zweben 268 posts 753 karma points
    Dec 22, 2013 @ 14:21
    David Zweben
    0

    Cool, glad to help.

Please Sign in or register to post replies

Write your reply to:

Draft