Copied to clipboard

Flag this post as spam?

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


  • Matthew 138 posts 201 karma points
    Apr 23, 2014 @ 21:42
    Matthew
    1

    Why does TypedMedia work in a view but not in a partial?

    I created a view that uses:

    @{
        var page = Model.Content;
        var mediaItem = Umbraco.TypedMedia(page.GetPropertyValue("featureImage"));
    }
    <div class="panel-img">
        <img src ="@mediaItem.GetPropertyValue("umbracoFile")" alt="" />
    </div>
    

    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?

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Apr 23, 2014 @ 22:13
    Jeavon Leopold
    100

    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.

    var mediaItem = Umbraco.TypedMedia(page.GetPropertyValue<int>("featureImage"));
    

    Jeavon

  • Matthew 138 posts 201 karma points
    Apr 23, 2014 @ 22:27
    Matthew
    0

    Hi Jeavon,

    The first line is:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    

    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!

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Apr 23, 2014 @ 23:00
    Jeavon Leopold
    0

    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

  • Matthew 138 posts 201 karma points
    Apr 24, 2014 @ 01:40
    Matthew
    0

    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:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{ Layout = "SectionPages.cshtml"; }
    
    @section Breadcrumb{ @Html.Partial("SiteBreadcrumb") }
    
    @{ var page = Model.Content; }
    
    <div class="row">
        <div class="col-xs-12 bkg-top-img" style="background-image: url(@page.GetCropUrl("topImage", "FullSizeBanner")); background-repeat: no-repeat;">
            <div class="bkg-top-img-text">
                <h1>@page.Name</h1>
            </div>
        </div>
        <div class="clearfix"></div>
    </div>
    <div class="row">
        <div class="col-xs-12">
            <h1>@page.GetPropertyValue("title")</h1>
            @page.GetPropertyValue("bodyText")
        </div>
    </div>
    
    @Html.Partial("DisplaySectorChildPages")
    

    The partial, with the offending var and GPV() additions commented out:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{ var nodes = Model.Content.Children.Where(x => x.IsVisible());
    
    @* IF I ADD THESE VARS HERE: *@
    var page = Model.Content;
    var mediaItem = Umbraco.TypedMedia(page.GetPropertyValue<int>("featureImage"));
    
        if (nodes.Any()){
            <ul class="list-unstyled page-list">
                @foreach (var childPage in nodes){
                <div class="row">
                        <li>
                            <div class="col-lg-4 col-md-6 col-sm-6 col-xs-12">
                                <div class="panel panel-default feature-box">
                                    <div class="panel-heading">
                                        <a href="@childPage.Url"><h4>@childPage.GetPropertyValue("Title")</h4></a>
                                    </div>
                                    <div class="panel-img">
                                        <a href="@childPage.Url">
    
                                        @*<img src ="/images/780ff.jpg" alt="" />*@
                                        @* AND REPLACE THE ABOVE WITH THE BELOW *@
                                        <img src="@mediaItem.GetPropertyValue("umbracoFile")" alt="" />
    
                                        </a>
                                    </div>
                                </div><!--/featuer box-->
                            </div><!--/col-->
                            <div class="col-lg-8 col-md-6 col-sm-6 col-xs-12">
                                <div class="panel-body">
                                    <a href="@childPage.Url">
                                        <h4>@childPage.GetPropertyValue("Title")</h4>
                                    </a>
                                    @childPage.GetPropertyValue("lead")
                                </div>
                            </div>
                        </li>
                    </div><!-- end row -->
                }
            </ul>
        }
    }
    

    I get an error: 'Object reference not set to an instance of an object.'

  • Matthew 138 posts 201 karma points
    Apr 24, 2014 @ 02:06
    Matthew
    0

    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.

  • Matthew 138 posts 201 karma points
    Apr 24, 2014 @ 06:18
    Matthew
    0

    Ok, trial and error with the Media section of the UmbracoHelper docs lead me to put this in the foreach:

    var mediaID = childPage.GetPropertyValue("featureImage");
    var mediaTyped = Umbraco.TypedMedia(mediaID);
    
    <a href="@childPage.Url">
    

    It works so for now I can move on but I still wish I knew why those other TypedMedia didn't.

  • Matthew 138 posts 201 karma points
    Apr 24, 2014 @ 07:04
    Matthew
    0

    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.

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Apr 24, 2014 @ 11:22
    Jeavon Leopold
    2

    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

    var a = Umbraco.TypedMedia(Model.Content.GetPropertyValue("thing"));
    

    and "thing" doesn't exist it causes the exception, doing a check should stop this, e.g.

    if (Model.Content.HasValue("thing"))
    {
        var a = Umbraco.TypedMedia(Model.Content.GetPropertyValue("thing"));
    }
    

    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

  • Matthew 138 posts 201 karma points
    Apr 24, 2014 @ 16:41
    Matthew
    0

    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.

  • Matthew 138 posts 201 karma points
    Apr 24, 2014 @ 17:10
    Matthew
    0

    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.

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Apr 24, 2014 @ 17:21
    Jeavon Leopold
    0

    No problem, glad it's making sense now

Please Sign in or register to post replies

Write your reply to:

Draft