6

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?

gunr2171
  • 16,104
  • 25
  • 61
  • 88
hncl
  • 2,295
  • 7
  • 63
  • 129

6 Answers6

6

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

Community
  • 1
  • 1
Alastair Pitts
  • 19,423
  • 9
  • 68
  • 97
  • I am new to MVC, here is the code: public static string ExTextBox(this HtmlHelper helper, string name, object value, bool readOnly, object htmlAttributes) { string HTML = ""; //if (readOnly) HTML = String.Format("", name, value); if (readOnly) HTML = value == null ? "" : value.ToString(); else HTML = System.Web.Mvc.Html.InputExtensions.TextBox(helper, name, value, htmlAttributes) + System.Web.Mvc.Html.ValidationExtensions.ValidationMessage(helper, name, "*"); return HTML; } – hncl Aug 31 '10 at 05:26
  • @hnabih: If this is the correct answer, don't forget to mark it as such :) – Alastair Pitts Aug 31 '10 at 07:18
4

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));
}
Anders
  • 734
  • 8
  • 12
1

@Anders method as an extension method. The nice thing about this is you can append several MvcHtmlStrings together along with other values (eg normal strings, ints etc) as ToString is called on each object automatically by the system.

    /// <summary>
    /// Concatenates MvcHtmlStrings together
    /// </summary>
    public static MvcHtmlString Append(this MvcHtmlString first, params object[] args) {
        return new MvcHtmlString(string.Concat(args));
    }

Example call:

MvcHtmlString result = new MvcHtmlString("");
MvcHtmlString div = new MvcHtmlString("<div>");
MvcHtmlString closediv = new MvcHtmlString("</div>");

result = result.Append(div, "bob the fish", closediv);
result = result.Append(div, "bob the fish", closediv);

It would be much nicer if we could overload operator+

mike nelson
  • 21,218
  • 14
  • 66
  • 75
0

Here is my way:

        // MvcTools.ExtensionMethods.MvcHtmlStringExtensions.Concat
        public static MvcHtmlString Concat(params MvcHtmlString[] strings)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            foreach (MvcHtmlString thisMvcHtmlString in strings)
            {
                if (thisMvcHtmlString != null)
                    sb.Append(thisMvcHtmlString.ToString());
            } // Next thisMvcHtmlString

            MvcHtmlString res = MvcHtmlString.Create(sb.ToString());
            sb.Clear();
            sb = null;

            return res;
        } // End Function Concat
Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442
0
        public static MvcHtmlString Concat(params MvcHtmlString[] value)
        {
            StringBuilder sb = new StringBuilder();
            foreach (MvcHtmlString v in value) if (v != null) sb.Append(v.ToString());
            return MvcHtmlString.Create(sb.ToString());
        }
N-ate
  • 6,051
  • 2
  • 40
  • 48
0

Working off of @mike nelson's example this was the solution that worked best for me:

No need for extra helper methods. Within your razor file do something like:

@foreach (var item in Model)
{
    MvcHtmlString num = new MvcHtmlString(item.Number + "-" + item.SubNumber);
    <p>@num</p>
}
Colin
  • 1,758
  • 1
  • 19
  • 24