我有一個幫手,我可以這樣調用沒有問題:MVC剃刀:助手導致html.actionlink
Helpers.Truncate(post.Content, 100);
然而,當我把它在一個@ Html.ActionLink我得到以下錯誤:
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
這是影響到了代碼:
@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>
}
我的助手代碼位於App_Code文件\ Helpers.cshtml和代碼如下:
@helper Truncate(string input, int length)
{
if (input.Length <= length)
{
@input
}
else
{
@input.Substring(0, length)<text>...</text>
}
}
ActionLink似乎沒有錯,但是你可以通過替換'Helpers.Truncate(post.Content,100)''例如''test「'來驗證ActionLink是否工作? – Marthijn
@Marthijn這也可以: @ Html.ActionLink(「Test」,「Topic」,new {TopicID = post.TopicID},null) –