2010-08-31 73 views
6

我的ASP.NET MVC应用程序转换为ASP.NET MVC 2 4.0型MvcHtmlString的操作数,并得到这个错误:操作 '+' 不能应用于

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, "*"); 

岂今要补救吗?

回答

5

您不能连接MvcHtmlString的实例。您需要将它们转换为普通字符串(通过.ToString())或以其他方式进行。

你可以写一个扩展方法为好,看到这个答案的例子:How to concatenate several MvcHtmlString instances

+0

我是新来的MVC,这里是代码: 公共静态字符串ExTextBox(此的HtmlHelper助手,字符串名称,对象的值,布尔只读的,对象htmlAttributes) { 字符串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,“*”); 返回HTML; } – hncl 2010-08-31 05:26:54

+0

非常感谢,它的工作 – hncl 2010-08-31 05:33:55

+0

@hnabih:如果这是正确的答案,请不要忘记标记为:) – 2010-08-31 07:18:01

4

我个人使用一个很苗条实用的方法,即采用在串级利用现有的毗连()方法:

public static MvcHtmlString Concat(params object[] args) 
{ 
    return new MvcHtmlString(string.Concat(args)); 
} 
+0

不错的工作,我喜欢它 – 2013-08-19 05:01:07

0

这里是我的方法:

 // 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 
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()); 
     } 
1

@A nders方法作为扩展方法。关于这一点的好处是,您可以将多个MvcHtmlStrings和其他值(例如普通字符串,整数等)一起附加到一起,因为系统会自动为每个对象调用ToString。

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

调用示例:

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

这将是更好,如果我们能重载operator +

+2

看起来你的'Append'方法放弃了'first'参数。 – recursive 2014-07-29 21:27:34

+0

确实。你应该使用'return new MvcHtmlString(first + string.Concat(args))'。 – Andrew 2015-10-30 06:15:50

0

关闭@mike尼尔森的例子的工作,这是为我工作最好的解决方案:

不需要额外的辅助方法。在你的剃须刀文件做这样的事情:

@foreach (var item in Model) 
{ 
    MvcHtmlString num = new MvcHtmlString(item.Number + "-" + item.SubNumber); 
    <p>@num</p> 
} 
相关问题