Well, if it helps, what I'm doing is going through my mock-up and replacing spots where I used static image references with (hopefully) umbraco/razor queried images, and I just tried another spot where it didn't work even with the <int>, this one in another partial view but also in a foreach:
By which I mean, in the above, that when I put those commented out bits in, I get that error, otherwise, it runs fine.
I also tried putting them down in the foreach loop, and when in there, either omitting the page var, or leaving it and renaming the TypedMedia from (page. to (childPage.
Also just tried renaming the 'page' var to 'thisPage' as I noticed it was also in the view so maybe was coming into the partial with some baggage.
If I do: @childPage.GetPropertyValue("featureImage") , just using the existing variable 'childPage' in the foreach, it returns the media id.
Hmm, yes, it makes sense, sort of. I don't get why it would have anything to do with that last case, with two identical partials (the only differences being the view they were called from and the size of the layout divs) but I confess I purposely left out if's thinking it'd save time during dev. But maybe not. I'll go put this to work and see. Thanks again.
Well, aside from seeing that my code may now become more 'if's than div's, that actually helps quite a bit, as it pointed out that there was an offending child content page that didn't even have a featureImage property, by design. So it turns out to be an ID10T error. Doh! I wondered why all you guys put so many 'if's in your sample code...
Thank you so much for taking the time to enlighten me.
Why does TypedMedia work in a view but not in a partial?
I created a view that uses:
Then I tried to use the same thing in a partial view that's called from w/in a different view, by a Html.Partial(...) but it blows an error:
'The value of parameter 'id' must be either a string or an integer'
Why is that? Aren't partial views just like views?
Hi Matthew,
What is the first line (the inherits) in your Partial View?
Regardless, specifying the type for GetPropertyValue should get it going, e.g.
Jeavon
Hi Jeavon,
The first line is:
and adding the
<int>
does get it going perfectly, so thanks much for that.Is this something I need to learn about declaring variables properly or is it about learning how to use @inherits?
Or both...
Thanks again!
Hi Matthew,
Actually it might be a bug, one way or the other, I will have a closer look.
However generally it is useful to specify the return type when using the GetPropertyValue method but not always necessary.
Jeavon
Well, if it helps, what I'm doing is going through my mock-up and replacing spots where I used static image references with (hopefully) umbraco/razor queried images, and I just tried another spot where it didn't work even with the
<int>
, this one in another partial view but also in a foreach:The view:
The partial, with the offending var and GPV() additions commented out:
I get an error: 'Object reference not set to an instance of an object.'
By which I mean, in the above, that when I put those commented out bits in, I get that error, otherwise, it runs fine.
I also tried putting them down in the foreach loop, and when in there, either omitting the page var, or leaving it and renaming the TypedMedia from (page. to (childPage.
Also just tried renaming the 'page' var to 'thisPage' as I noticed it was also in the view so maybe was coming into the partial with some baggage.
If I do:
@childPage.GetPropertyValue("featureImage")
, just using the existing variable 'childPage' in the foreach, it returns the media id.Ok, trial and error with the Media section of the UmbracoHelper docs lead me to put this in the foreach:
It works so for now I can move on but I still wish I knew why those other TypedMedia didn't.
And now, on another partial that is functionally identical, that last bit that worked, doesn't. Just shoot me...
'Object reference not set to an instance of an object. '
This time referring to a 'Title' property, not an image.
Hi Matthew,
I've had a good look and the reason is this is that when you pass in the value of object like this
and "thing" doesn't exist it causes the exception, doing a check should stop this, e.g.
When you pass in the int, if the conversion failed, you are passing a null which TypedMedia will accept which is why it doesn't cause the exception.
Generally, make sure you check for HasValue before passing that into TypedMedia.
Hope that make sense?
Jeavon
Hmm, yes, it makes sense, sort of. I don't get why it would have anything to do with that last case, with two identical partials (the only differences being the view they were called from and the size of the layout divs) but I confess I purposely left out if's thinking it'd save time during dev. But maybe not. I'll go put this to work and see. Thanks again.
Well, aside from seeing that my code may now become more 'if's than div's, that actually helps quite a bit, as it pointed out that there was an offending child content page that didn't even have a featureImage property, by design. So it turns out to be an ID10T error. Doh! I wondered why all you guys put so many 'if's in your sample code...
Thank you so much for taking the time to enlighten me.
No problem, glad it's making sense now
is working on a reply...