locked
Help with this Helper RRS feed

  • Question

  • User1463147114 posted

    Hi. I would appreciate some help in getting this Helper working.

    I have a folder App_Code and in it Helpers.cshtml. In it is the code for Truncate helper:

    @helper Truncate(string input, int length)
    {
        if (input.Length <= length) {
            @input
        } else {
            @input.Substring(0, length)
        }
    }

    And the code in my webgrid:

    grid.Column("MemberComment", "Member Comment", format: @@Html.Truncate(@item.MemberComment, 200)),
    

    This errors with: CS1646: Keyword, identifier, or string expected after verbatim specifier: @

    I tried various combinations of @Html, @@Html, @item, item and <text></text> with no luck.

    Any suggestions?

    Saturday, August 17, 2013 12:10 PM

Answers

  • User1853794821 posted

    Does @Helpers.Truncate help?

    You may be better served by defining a function for the truncate, as opposed to an Html helper.  The function would return one of the two possible results shown in your helper.  They would be returned to the code as a string and the calling code can then determine how the results are displayed/formatted.

    You will also want to put a test for  input==null in your helper

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 17, 2013 12:36 PM

All replies

  • User-183374066 posted

    Try like

    grid.Column("MemberComment", "Member Comment", format: @(Html.Truncate(Convert.ToString(item.MemberComment), 200))),
    Saturday, August 17, 2013 12:28 PM
  • User1853794821 posted

    Does @Helpers.Truncate help?

    You may be better served by defining a function for the truncate, as opposed to an Html helper.  The function would return one of the two possible results shown in your helper.  They would be returned to the code as a string and the calling code can then determine how the results are displayed/formatted.

    You will also want to put a test for  input==null in your helper

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 17, 2013 12:36 PM
  • User1463147114 posted

    Hi Nasser.

    Thanks for the suggestion but I get the same error with this, i.e. CS1646: Keyword, identifier, or string expected after verbatim specifier: @

    Saturday, August 17, 2013 12:52 PM
  • User1463147114 posted

    HI.

    Yes this approach works - thanks!

    Here's the final code:

    grid.Column("MemberComment", "Member Comment", format:@<text>@(Helpers.Truncate(Convert.ToString(item.MemberComment), 200)))</text>),

    I'm still quite new when it comes to fuctions and helpers, obviously, but I'll give your suggestion a try.

    Thx again.

    Saturday, August 17, 2013 12:59 PM
  • User-821857111 posted

    rrrsr7025 is correct. You should define this as a function not a helper. For more infomration on the difference between the two, see this: http://www.mikesdotnetting.com/Article/173/The-Difference-Between-@Helpers-and-@Functions-In-WebMatrix

    Saturday, August 17, 2013 1:42 PM
  • User1463147114 posted

    Just in case there are other newbies like myslef interested in defining functions here is my Truncate function:

    public static HtmlString Truncate(string value, int length){
            if(value == null || value.Length <= length){
                return new HtmlString(value);
                } else {
                 return new HtmlString(value.Substring(0, length)); 
                }
        }  

    And the code to use it in webgrid:

    grid.Column("Comment", format:@<text> @(Functions.Truncate(Convert.ToString(item.Comment), 200))</text>),
    
    Sunday, August 18, 2013 4:13 PM