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
Member
25 Points
85 Posts
Help with this Helper
Aug 17, 2013 12:10 PM|jw7965|LINK
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:
And the code in my webgrid:
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?
Star
13450 Points
2764 Posts
Re: Help with this Helper
Aug 17, 2013 12:28 PM|Nasser Malik|LINK
Try like
Skype: maleknasser1
LinkedIn: https://www.linkedin.com/in/maliknasser
Participant
1271 Points
452 Posts
Re: Help with this Helper
Aug 17, 2013 12:36 PM|rrrsr7205|LINK
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
Member
25 Points
85 Posts
Re: Help with this Helper
Aug 17, 2013 12:52 PM|jw7965|LINK
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: @
Member
25 Points
85 Posts
Re: Help with this Helper
Aug 17, 2013 12:59 PM|jw7965|LINK
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.
All-Star
194025 Points
28032 Posts
Moderator
Re: Help with this Helper
Aug 17, 2013 01:42 PM|Mikesdotnetting|LINK
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
Member
25 Points
85 Posts
Re: Help with this Helper
Aug 18, 2013 04:13 PM|jw7965|LINK
Just in case there are other newbies like myslef interested in defining functions here is my Truncate function:
And the code to use it in webgrid: