Copied to clipboard

Flag this post as spam?

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


  • wolulcmit 357 posts 693 karma points
    Jul 04, 2013 @ 11:07
    wolulcmit
    1

    Calling Umbraco.Media from a nested Partial

    I'm having a problem with using Umbraco.Media from a nested partial (partial within a partial)

    similar to this example: http://our.umbraco.org/Documentation/Reference/Mvc/partial-views

    my code goes something like

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    @{
    // Main Content getting a value from a content picker
    var content = Model.Content.GetPropertyValue("mainContent").ToString();
    string[] nodeIds = content.Split(',');

    //looping through my nodes
    foreach (var node in nodeIds)
    {
    var realNode = Umbraco.TypedContent(node);

    if(realNode.HasProperty("bodyText")){
    //render bodytext etc
    }else if(realNode.HasProperty("slideShowImages")){
    //render slideshow
    @Html.Partial("slideshow", realNode)
    }
    }

    }

    And then in my slideshow.chtml partial:

    @model IPublishedContent

    @{
    var Slideshow = Model.GetPropertyValue("slideshowImages").ToString();
    string[] nodes = Slideshow.Split(',');
    foreach(var nodeId in nodes){
    var media = Umbraco.Media(nodeId);
    var defaultClass = "class=\"default\"";
    <img src="@media.url" />
    }

    But this gives me the error:

    The type or namespace name 'Media' does not exist in the namespace 'Umbraco' (are you missing an assembly reference?

    How do I re-reference the correct Namespace so that Umbraco.Media works within my nested partial?
    I've tried @using Umbraco, Umbraco.Web, Umbraco.Web.Media

    is it possible to use Umbraco.Media at the nested level or should I give up?

    thanks!

    - Tim

  • wolulcmit 357 posts 693 karma points
    Jul 04, 2013 @ 11:11
    wolulcmit
    0

    By the way I'm using Umbraco 6.1.2 in Mvc Mode

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Jul 04, 2013 @ 21:58
    Andy Butland
    102

    I think using @inherits rather than @model on your nested partial should work, like this:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedContent>

    You'll then have access to the @Umbraco helper.

    Andy

  • wolulcmit 357 posts 693 karma points
    Jul 05, 2013 @ 02:17
    wolulcmit
    0

    @Andy Thankyou Thankyou Thankyou!

    worked! hopefully anyone else out there having trouble with find this post :)

  • Eric Schrepel 161 posts 226 karma points
    May 13, 2014 @ 23:27
    Eric Schrepel
    0

    Would I use the same @inherit statement at the top of a Global Helper when I'm getting the same "The type or namespace name 'Media' does not exist in the namespace 'Umbraco'" error? For example, in App_Code/GlobalHelpers.cshtml, I created this helper which works fine when it's in a Partial View, but not when I put it into the global helper:

    @helper GetCropUrl(dynamic imageName, string cropName ){
        @* get crop URL name from image and crop name *@

    var image = Umbraco.Media(imageName); @* this is the line causing the error *@ var imglink = image.url; var imgcrop = image.croppedImage; if (imgcrop.ToString() != "") { if (imgcrop.Find("@name", cropName).BaseElement.ToString() != "") { imglink = imgcrop.Find("@name", cropName).url; } } @imglink }*@
  • Burki 7 posts 77 karma points
    Jan 04, 2017 @ 07:57
    Burki
    0

    Hi Eric,

    You can use a custom static variable in your helper. Here's how I sort it out.

    @functions
    {
        public static Umbraco.Web.UmbracoHelper umbracoHelper = new   Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
    }
    
    @helper MyFunction(dynamic page){
      var mediaItem = umbracoHelper.Media(page.Image)
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft