Copied to clipboard

Flag this post as spam?

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


  • Peter Cort Larsen 421 posts 1038 karma points
    Nov 14, 2020 @ 11:11
    Peter Cort Larsen
    0

    Global razor helper umb 8

    Hi,

    I have made a cshtml file in App_Code with global helper methods.

    The problem is that i get an exception after build.

    Compilation 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: CS0534: 'helper' does not implement inherited abstract member 'WebPageExecutingBase.Execute()'
    
    Source Error:
    
    Line 50:     public class helper : UmbracoViewPage {
    

    My code look like this

    @using System.Text.RegularExpressions
    @using Umbraco.Core.Models.PublishedContent;
    @inherits UmbracoViewPage
    
    @helper RenderImage(dynamic i)
    {
        var picture = "";
        if (i.HasValue("image"))
        {
            picture = i.Value<IPublishedContent>("image").Url();
            <img src="@picture" class="img-resp" />
        }
    }
    

    Can anyone help?

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Nov 14, 2020 @ 21:34
    Marc Goodson
    0

    Hi Peter

    I'm not sure you need to inherit UmbracoViewPage at the top?

    @inherits UmbracoViewPage
    

    essentially you are saying that your @helper class inherits from UmbracoViewPage and this requires you to implement WebPageExecutingBase.Execute()

    Which you 'could' do, but I think if you remove the @inherits statement then you won't need to and your helper will still work...

    regards

    Marc

  • Peter Cort Larsen 421 posts 1038 karma points
    Nov 15, 2020 @ 12:38
    Peter Cort Larsen
    0

    Hi,

    Thanks that helped.

    I have another problem after this.

    In the global helper, how do i convert the dynamic object thats passed into the method into the correct type?

    I tried something like this, but thats not correct

    @helper RenderImage(object i)
    {
    
        var type = i.GetType();
    
        var sc = (type)i;
    
    
            if (!string.IsNullOrEmpty(sc.Image.ToString()))
            {
                IPublishedContent picture = sc.Image;
                <img src="@picture.Url" class="img-resp" />
            }
    }
    
  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Nov 15, 2020 @ 21:35
    Søren Gregersen
    0

    Hi,

    The line var type = i.GetType(); gets information about a type, but not a type. In c# types are static, which means you need have it available at compile time.

    Your RenderImage could be rewritten as:

    @helper RenderImage(IPulishedContent c, string imageAlias = "Image")
    {
        if(c==null) return;
        var image = c.Value<IPublishedContent>(imageAlias);
        if(image==null) return;
    
         <img src="@picture.Url" class="img-resp" />
    }
    

    Another rewrite could be:

    @helper RenderImage(IContentWithImage c)
    {
        if(c==null || c.Image == null) return;
    
         <img src="@c.Image.Url" class="img-resp" />
    }
    

    IContentWithImage would be a composition "Content With Image" that defines the Image-property

  • Peter Cort Larsen 421 posts 1038 karma points
    Nov 15, 2020 @ 12:39
    Peter Cort Larsen
    0

    I know this is a very simple example, i have other more complex stuff i would like to have in a global helper file.

  • Peter Cort Larsen 421 posts 1038 karma points
    Nov 16, 2020 @ 08:16
    Peter Cort Larsen
    0

    Hi ,

    Thanks,

    The problem is that i dont know the model beforehand, but all the models that use the helper has the same property.

  • Peter Cort Larsen 421 posts 1038 karma points
    Nov 16, 2020 @ 08:19
    Peter Cort Larsen
    0

    The calling razor looks like this:

    @using MYMODELNAMESPACE.Models.ModelsBuilder;
    @using Perplex.ContentBlocks.Rendering;
    
    @model IContentBlockViewModel<TwoColumnImageVideoElement>
    @{
        var imageSlider = Model.Content.FirstColumn;
    }
    @if (imageSlider.Any())
    {
        foreach (var i in imageSlider)
        {
            var images = i.ImageItems;
            foreach (var img in images)
            {
                if (img.HasValue("image"))
                {
                    var RenderImage = helper.RenderImage(img);
                    @RenderImage
                }
    
            }
        }
    }
    
  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Nov 16, 2020 @ 08:26
    Marc Goodson
    100

    Hi Peter

    What is the type of imageSlider and i.ImageItems?

    IEnumerable<IPublishedContent> or IEnumerable<IPublishedElement> ??

    It's hard to tell, but it's likely your img, implements IPublishedContent so you could probably use that as your type in your razor helper.

    @helper RenderImage(IPublishedContent i)
    

    ??

    If your razor helper, is rendering HTML would it be better to have another PartialView for rendering images, that you called like Html.Partial("RenderImagePartial", model);

    rather than a razor helper?

    or perhaps an extension method on HtmlHelper, so you could call it in your views like @Html.RenderImageHelper(model): https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/views/creating-custom-html-helpers-cs

    regards

    Marc

  • Peter Cort Larsen 421 posts 1038 karma points
    Nov 16, 2020 @ 10:25
    Peter Cort Larsen
    1

    Hi,

    yes, this is the way.

     public static string RenderImage(IPublishedContent Image)
        {
            if (Image != null)
            {
                return String.Format("<img src='{0}' class='img-resp'/>", Image.Url());
            }
            return string.Empty;
        }
    

    Then i can call it like this:

       @Html.Raw(UmbracoHelperExtensions.RenderImage(i.Image))
    

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft