Copied to clipboard

Flag this post as spam?

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


  • Tim C 161 posts 528 karma points
    Jun 12, 2020 @ 11:17
    Tim C
    0

    Using Umbraco.helper in a partial view @functions

    In a prtial view, I have some code in an

    @functions{}
    

    section which tries to use Umbraco.Helper to get the url of a media image given its id. ie.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    ...
    
    @functions{
    
        public class rowOutput{
                ...
                int mediaId = Int32.Parse(pValue);
           umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);       pValue = umbracoHelper.TypedMedia(mediaId).Url.ToString();
           ...
           }
    ...
    }
    

    but I get this error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS0103: The name 'umbracoHelper' does not exist in the current context

    Source Error:

    Line 641: int mediaId = Int32.Parse(pValue); Line 642: umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
    Line 643: pValue = umbracoHelper.TypedMedia(mediaId).Url.ToString();

    How can I add reference to Umbraco.Helper in this partial view?

    Many thanks

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Jun 13, 2020 @ 09:54
    Marc Goodson
    0

    Hi Tim C

    I think this is just because the umbracoHelper variable isn't declared in your function eg:

     int mediaId = Int32.Parse(pValue);
     var umbracoHelper = new 
     Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
     pValue = umbracoHelper.TypedMedia(mediaId).Url.ToString();
    

    But equally you can access the UmbracoHelper from the property on the partial view eg:

     int mediaId = Int32.Parse(pValue);
     pValue = Umbraco.TypedMedia(mediaId).Url.ToString();
    

    Would have the same result, but not involve creating a new helper...

    regards

    marc

Please Sign in or register to post replies

Write your reply to:

Draft