Passing models to partial views
# help-with-umbraco
s
Morning all, apologies for the noob-y question, but how do you go about passing a model's data into a partial view? In a project I am working on, I have a home page banner data (which uses a composition), and this is my banner partial. The home page 'Title' and 'Subtitle' render perfectly fine, but when I try to render an image - using
image.Image.Url()
or
image.Url()
- I get an error. Any tips/advice would be much appreciated, TIA! https://cdn.discordapp.com/attachments/1240254835916541954/1240254836541227089/image.png?ex=6645e4d3&is=66449353&hm=d3fdd4e94d9540133141a097d5c4b41aa3464b56493ab16bc65054762424afa2& https://cdn.discordapp.com/attachments/1240254835916541954/1240254836847546368/image.png?ex=6645e4d3&is=66449353&hm=f841fc296b752a46229c4128deb773ed7358de4c931f4119a9494701fc50d03c& https://cdn.discordapp.com/attachments/1240254835916541954/1240254837074169918/image.png?ex=6645e4d3&is=66449353&hm=8e5aca92fdd6d2f868e7e2e3060f454fa1d0d6a7f2c6c44ee18d3015045380ae&
m
what error do you get with
image.Url()
? (this is the one Id expect to work)
m
@using Umbraco.Extensions
try adding this to the top of your file
it should be in yout Views/_ViewImports.cshtml already though
s
Yeah, it is already in
Views/_ViewImports.cshtml
- just tried adding it to the Banner partial, but no luck 😦
m
image.Content.Url()
n
What does the entire banner.cshtml file look like? Is it complete in the screenshot you've shared?
s
Hiya Nik! Yeah the banner screenshot I've posted is the full partial
n
Is banner designed to be reusable, or is it only being used with the home page?
s
It'll be reusable, for text pages, a contact page, etc
n
Okay, so what you've got needs a bit of work to make it reusable I think. How have you added the properties to the home page? Did you create a composition for "Banner properties" by any chance and then have your home page have it as one of it's compositions?
s
Yeah exactly that, I've got a 'banner properties' composition that the homepage inherits
n
Cool, okay.. so "this" should work I think: Add this to the very top of your banner cshtml file
Copy code
cs
@inherits UmbracoViewPage<IBannerProperties> //Assuming that bannerProperties is the alias for the composition itself
s
Ahh yeah that has done the trick, thanks Nik!
8 Views