Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Hello. I'm trying to pass some parameters to a partial, but i can't figure out how to do it it.
What i want to achieve
I want to be able to use the same partial multiple times in a template, but pass it the value of different aliases each time.
lets say i have a partial which renders a textstring and textarea.
I would like to be able to do something like this
@await Html.PartialAsync("PartialName", name = "@Model.Value("title"), image = "@Model.Value("text")") @await Html.PartialAsync("PartialName", name = "@Model.Value("IntroTitle"), image = "@Model.Value("IntroText")")
To render this content
If i had an Array of items i would do this, but that is not the case.
@foreach (var item in collection) { @await Html.PartialAsync("PartialName", item) }
I found this in the docs, but i can't figure out how to make it work.
https://our.umbraco.com/Documentation/Reference/Templating/Mvc/examples#:~:text=Rendering%20a%20macro%20with%20parameters%20using%20an%20anonymous%20object
you should be able to pass it as a viewdata object
new { name = "@Model.Value("IntroTitle"), image = "@Model.Value("IntroText")" }
Then access it in the partial
ViewData["name"];
I cant seem to make that work, im sure im doing something completely wrong :)
Render:
@await Html.PartialAsync("PartialName", new { name = "Thomas"})
Inside partial
var name = ViewData["name"]; <p>@name</p>
I just checked my code and I am doing this to pass the data
new ViewDataDictionary(this.ViewData) { { "Name", "Checkout" }, { "Header", "Checkout" } }
Then in Partial
var name = ViewData["Name"].ToString();
TBH, I have switched most of my partials to use viewcomponents in Umbraco 9+ so most of my partials are just static html, anything requiring data to be passed I have moved to viewcomponents.
But the example above is still working :)
Thanks alot! I'll take a look at viewcomponents, sounds like that is a better solution ;)
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Passing parameters to a partial
Hello. I'm trying to pass some parameters to a partial, but i can't figure out how to do it it.
What i want to achieve
I want to be able to use the same partial multiple times in a template, but pass it the value of different aliases each time.
lets say i have a partial which renders a textstring and textarea.
I would like to be able to do something like this
To render this content
If i had an Array of items i would do this, but that is not the case.
I found this in the docs, but i can't figure out how to make it work.
https://our.umbraco.com/Documentation/Reference/Templating/Mvc/examples#:~:text=Rendering%20a%20macro%20with%20parameters%20using%20an%20anonymous%20object
you should be able to pass it as a viewdata object
Then access it in the partial
I cant seem to make that work, im sure im doing something completely wrong :)
Render:
Inside partial
I just checked my code and I am doing this to pass the data
Then in Partial
TBH, I have switched most of my partials to use viewcomponents in Umbraco 9+ so most of my partials are just static html, anything requiring data to be passed I have moved to viewcomponents.
But the example above is still working :)
Thanks alot! I'll take a look at viewcomponents, sounds like that is a better solution ;)
is working on a reply...