Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MvcHtmlString MVC 2 Conversion Error

I converted my project from MVC 1 to MVC 2 and Visual Studio 2008 gives me the following error:

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

Here is the code the error is pointing to. It is specifically having trouble with the "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;
     }

I suspect that it has something to do with a missing reference or something but I'm at a loss.

like image 908
Aaron Salazar Avatar asked Mar 04 '10 21:03

Aaron Salazar


1 Answers

Html.ActionLink() does no longer return a string. It now returns a MvcHtmlString. A MvcHtmlString does not have a method called .Substring() (only string does). If you call .ToString() or .ToHtmlString() (will encode the value) then you will be able to call .Substring(). See this link.

like image 150
Mattias Jakobsson Avatar answered Dec 30 '22 19:12

Mattias Jakobsson