Copied to clipboard

Flag this post as spam?

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


  • Tom 713 posts 954 karma points
    Aug 22, 2011 @ 01:35
    Tom
    0

    Global Helper Exception?

    Hi Guys,

    trying to implement a Global helper to help with reuse.. I'm getting an exception:

    Compiler Error Message: CS0534: 'ASP.GlobalHelpers' does not implement inherited abstract member 'System.Web.WebPages.WebPageExecutingBase.Execute()'

     

    I was just wondering if anyone else had come across this?

    my helper file is called GlobalHelpers.cshtml and contains:

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @using umbraco.MacroEngines

    @using MyProject.Web

    @using System.Collections.Generic;


    @helper WriteTable(string title, Dictionary<DynamicNode, DateTime?> nodes)

    {

     if (nodes.Count > 0)

     {

     <h3><span>@title</span></h3>

     <table>

     <thead>

     <tr>

    <th scope="col" class="required-col"></th>

    <th scope="col" class="title-col">Title</th>

    <th scope="col" class="type-col">Type</th>

    <th scope="col" class="date-col">Date Published</th>

    <th scope="col" class="date-col">Date Viewed</th>

     </tr>

    </thead>

    <tbody>

    @{

    int itemCount = 0;

    foreach (var node in nodes)

    {

    <tr class="@Html.Raw(@itemCount % 2 == 0 ? "" : "alt")">

    <td class="rating-col"><img src="/media/@Html.Raw(node.Key.GetProperty("required").Value == "1" ? "yellow" : "blue")-star.png" width="24" height="23" alt="star"/></td>

    <td><a href="@node.Key.Url">@node.Key.Name</a></td>

    <td><img src="/media/@{@RazorExtensions.Icon(node.Key);}.png" width="27" height="33" alt="@RazorExtensions.Icon(node.Key)"/></td>

    <td>@node.Key.UpdateDate.ToString("dd/MM/yy")</td>

    <td>@if (node.Value.HasValue)  { @Html.Raw(node.Value.Value.ToString("dd/MM/yy")); }</td>

    </tr>

    itemCount++;

    }

    }

    </tbody>

    </table>

    }

    }

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Sep 05, 2011 @ 22:16
    Dan Diplo
    0

    Where have you put the helper? As far as I know your helper classes have to go in App_Code.

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Sep 12, 2011 @ 18:09
    Sebastiaan Janssen
    0

    You're inheriting from DynamicNodeContext (why? do you really need this?), but for that to work you have to implement the Execute() method (DynamicNodeContext inherits from BaseContext, which inherits from WebPage that inherits from WebPageBase... which requires you to implement an Execute() method):

        public override void Execute()
        {
          // do something here
        }

    But I believe you're probably not using the inherit anyway, so you might as well remove it.

  • psiho 101 posts 96 karma points
    Dec 08, 2011 @ 01:19
    psiho
    0

    I'm bumping this topic up as I'm having the same problem. To shorten my global helper, let's say I need it only ot return some property of current page:

    @helper Test(string propertyName)  {
       Model.GetProperty(propertyName);
    }

    For this I also inherit umbraco.MacroEngines.DynamicNodeContext and also get the same error. I'm not sure ho to implement Execute method (Razor/C# noob) but simple copy/paste and replacing comment does not help.

     

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Dec 08, 2011 @ 08:16
    Sebastiaan Janssen
    0

    This you should not do with a helper but a function:

    @functions {
    public string(dynamic model, string propertyName) {
    return model.GetPropertyValue(propertyName);
    }

    A helper directly renders some content but can not return a value to your caller, this will render the bodytext, for example:

    <div class="body">
    @Test("bodyText")
    </div>

    @helper Test(string propertyName) {
    @Model.GetPropertyValue(propertyName)
  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Dec 08, 2011 @ 09:42
    Dan Diplo
    0

    To add to what Seb has said (which is totally correct) you can also just use plain old C# classes for helper functions that simply return values. You only need to use @helper methods if you want to render markup, and @functions are useful inside a macro, but if you want more generic libraries then use a standard C# class library. You can then pass your "Model" (DynamicNode) into your method to access its methods and properties (as Seb nicely illustrates).

  • psiho 101 posts 96 karma points
    Dec 08, 2011 @ 13:53
    psiho
    0

    Thx Sebastian! What I was trying to do is implement Global helper (function) to help me pull out correct data from Dictionary DataType (Multilanguage text field). So, at the end I have put this in file "GlobalHelpers.cshtml" in folder "App_Code":

     

    @functions {
    public static string getTranslatedValue(dynamic Model, string propertyName) {
    XElement xmlParser = XElement.Parse(Model.GetProperty(propertyName).Value.ToString());
    IEnumerable query = from array in xmlParser.Elements("value") select array;
    foreach(XElement rep in query) {
    if (System.Threading.Thread.CurrentThread.CurrentCulture.Name == rep.FirstAttribute.Value.ToString()) {
    return rep.Value;
    }
    }
    return "";
    }
    }

    So, in my macros, I just use @GlobalHelpers.getTranslatedValue(@Model,"fieldAlias") and that's it!

    @Dan... "plain old C# classes" is plain old for my programmers and not for me. I'm convert from XSLT and some basic Razor is as far as I will go. That is exactly the reason we love Umbraco so much, we can build complex (and flexible) websites with no help of developers which are usualy more expensive resources and harder to find.

     

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Dec 08, 2011 @ 14:14
    Dan Diplo
    0

    "@Dan... "plain old C# classes" is plain old for my programmers and not for me. "

    Fair enough, but actually the hard bit is writing the function, not the semantics of where it is placed! You could as easily have written a static class called Helpers.cs and placed it in App_Code and it would have worked identically. If you end up using the same helpers in multiple projects you can then compile them to an assembly and re-use by adding to the /bin folder. 

     


     

Please Sign in or register to post replies

Write your reply to:

Draft