1

I have a helper which I can call like this with no problem:

Helpers.Truncate(post.Content, 100);

However when I call it in a @Html.ActionLink I get the following 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: CS1928: 'System.Web.Mvc.HtmlHelper<System.Collections.Generic.IEnumerable<TRN.DAL.Post>>' does not contain a definition for 'ActionLink' and the best extension method overload 'System.Web.Mvc.Html.LinkExtensions.ActionLink(System.Web.Mvc.HtmlHelper, string, string, object, object)' has some invalid arguments

This is the code affected:

@foreach (var post in Model)
{
    <li>
        @Html.ActionLink(Helpers.Truncate(post.Content, 100), "Topic", new { TopicID = post.TopicID }, null)
        <p>By @Html.ActionLink(post.Username, "Members", new { MemberID = post.MemberID }, null) on @post.CreatedOn</p>
    </li>
}

My helper code is located in App_Code\Helpers.cshtml and the code is below:

@helper Truncate(string input, int length)
{
    if (input.Length <= length)
    {
        @input
    }
    else
    {
        @input.Substring(0, length)<text>...</text>
    }
}
Three Value Logic
  • 1,102
  • 1
  • 15
  • 37
  • There seems nothing wrong with the ActionLink, but can you verify if the ActionLink works by replacing `Helpers.Truncate(post.Content, 100)` by for example `"test"`? – Marthijn Dec 19 '13 at 14:27
  • @Marthijn this also works: @Html.ActionLink("Test", "Topic", new { TopicID = post.TopicID }, null) – Three Value Logic Dec 19 '13 at 14:28

2 Answers2

4

Try this:

@Html.ActionLink(Truncate(post.Content, 100).ToString(), "Home")

@helper Truncate(string input, int length)
{
    if (input.Length <= length)
    {
        @Html.Raw(input)
    }
    else
    {
         @Html.Raw(input.Substring(0, length) + "...")
    }
}
James
  • 2,195
  • 1
  • 19
  • 22
  • That solved it! Despite the cryptic message it was because My helper returned helperresult and the first param in Actionlink expects a string. – Three Value Logic Dec 19 '13 at 14:38
1

I suggest to change the helper function into a static function in a class of your choice. For example:

public static string Truncate(string input, int length)
{
    if (input.Length <= length)
    {
        return input;
    }
    else
    {
        return input.Substring(0, length) + "...";
    }
}

The in your view you use:

@Html.Actionlink(MyNamespace.MyClass.Truncate(input, 100), ...

Optionally you can change this function into an extension of string, there are plenty examples on how to do that:

public static string Truncate(this string input, int length) ...
Marthijn
  • 3,292
  • 2
  • 31
  • 48
  • Almost there. That comes up with: `CS0127: Since 'System.Action' returns void, a return keyword must not be followed by an object expression` – Three Value Logic Dec 19 '13 at 14:32
  • I forgot that it's a little bit complicated for helper functions to return a value (see this post: http://stackoverflow.com/questions/10451036/can-i-return-a-string-using-the-helper-syntax-in-razor) I suggest you make a static function in a class for this function. Ill update my post. – Marthijn Dec 19 '13 at 14:37
  • Marked as answer as I have gone with this strategy for my project. Thanks! – Three Value Logic Dec 19 '13 at 14:59