Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator '+' cannot be applied to operands of type MvcHtmlString

I am converting an ASP.NET MVC application to ASP.NET MVC 2 4.0, and get this error:

Operator '+' cannot be applied to operands of type 'System.Web.Mvc.MvcHtmlString' and 'System.Web.Mvc.MvcHtmlString'

HTML = Html.InputExtensions.TextBox(helper, name, value, htmlAttributes) 
       + Html.ValidationExtensions.ValidationMessage(helper, name, "*");

How can this be remedied?

like image 627
hncl Avatar asked Aug 31 '10 05:08

hncl


2 Answers

You can't concatenate instances of MvcHtmlString. You will either need to convert them to normal strings (via .ToString()) or do it another way.

You could write an extension method as well, see this answer for an example: How to concatenate several MvcHtmlString instances

like image 70
Alastair Pitts Avatar answered Sep 25 '22 11:09

Alastair Pitts


Personally I use a very slim utility method, that takes advantage of the existing Concat() method in the string class:

public static MvcHtmlString Concat(params object[] args)
{
    return new MvcHtmlString(string.Concat(args));
}
like image 30
Anders Avatar answered Sep 24 '22 11:09

Anders