MVC剃刀:助手导致html.actionlink [英] MVC Razor: Helper result in html.actionlink

查看:270
本文介绍了MVC剃刀:助手导致html.actionlink的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个帮手,我可以这样调用没有问题:

  Helpers.Truncate(post.Content,100 ); 



然而,当我把它在一个@ Html.ActionLink我得到以下错误:



 说明:该请求提供服务所需资源的编译过程中出现错误。请检查下面的特定错误详细信息并适当地修改源代码。 

编译器错误信息:CS1928:'System.Web.Mvc.HtmlHelper< System.Collections.Generic.IEnumerable< TRN.DAL.Post>>'不包含定义'ActionLink的和最佳扩展方法重载'System.Web.Mvc.Html.LinkExtensions.ActionLink(System.Web.Mvc.HtmlHelper,字符串,字符串,对象,对象)'具有一些无效参数

这是受影响的代码:

  @foreach (在型号后VAR)
{
<立GT;
@ Html.ActionLink(Helpers.Truncate(post.Content,100),主题,新的{TopicID = post.TopicID},NULL)
< P>通过@ Html.ActionLink(后.Username,对@ post.CreatedOn<成员,新的{MEMBERID = post.MemberID},NULL); / p>
< /李>
}



我的助手代码位于App_Code\Helpers.cshtml和代码如下:

  @helper截断(字符串输入,INT长度)
{
如果(input.Length < =长度)
{
@input
}
,否则
{
@ input.Substring(0,长度)LT;文本> .. < /文字和GT;
}
}


解决方案

我建议改变助手功能为静态函数在一个类你的选择。例如:

 公共静态字符串截断(字符串输入,INT长度)
{
如果(输入。长度< =长度)
{
返回输入;
}
,否则
{
返回input.Substring(0,长度)+...;
}
}



在你看来,你使用:



  @ Html.Actionlink(MyNamespace.MyClass.Truncate(输入100),... 

您也可以选择更改此功能为字符串的延伸,有关于如何大量实例做到这一点:

 公共静态字符串截断(此字符串输入,INT长度)... 


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>
    }
}

解决方案

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) ...

这篇关于MVC剃刀:助手导致html.actionlink的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆