MvcHtmlString MVC 2转换错误

Aar*_*zar 9 model-view-controller asp.net-mvc-2

将我的项目从MVC 1 转换为MVC 2,Visual Studio 2008给出了以下错误:

Error   1   'System.Web.Mvc.MvcHtmlString' does not contain a definition for 'Substring' and no extension method 'Substring' accepting a first argument of type 'System.Web.Mvc.MvcHtmlString' could be found (are you missing a using directive or an assembly reference?) C:\Dev\SapientFansite\SapientFansiteApplication\SapientFansiteWeb\Code\ExtensionMethods\Html.cs 68  75  SapientDevelopment.SapientFansite.Web
Run Code Online (Sandbox Code Playgroud)

这是错误指向的代码.特别是"linkHtml.Substring(0,2)"有问题.

     var linkHtml = htmlHelper.ActionLink(linkText, actionName, controllerName);
     if (isActiveMenuItem) {
        linkHtml = string.Format("{0} class=\"active\" {1}", linkHtml.Substring(0, 2), linkHtml.Substring(3));
     }
     return linkHtml;
     }
Run Code Online (Sandbox Code Playgroud)

我怀疑它与缺少的参考或某事有关,但我不知所措.

Mat*_*son 11

Html.ActionLink()不再返回字符串.它现在返回一个MvcHtmlString.MvcHtmlString没有调用的方法.Substring()(只有字符串).如果你打电话.ToString().ToHtmlString()(将对值进行编码),那么你就可以打电话了.Substring().看到这个链接.