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.
}
}
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>
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.
How do you pass parameters to Partial View Macro Files in 7.01
I am calling a Partial View Macro
And I need to get the value of HomePageGalleryId
If I do this
I see 0, which is a worry.
If I do this, it crashes as there probably isn't any MacroParameters
How do I get the HomePageGalleryId passed to the Partial View Macro?
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. } }
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:
This returns "Not there" and a Count of 0
So the parameter is not being passed. I also tried:
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
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.
No, I forgot all about that! Now it works.
Thanks David.
Cool, glad to help.
is working on a reply...