2010-08-31 78 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> 
} 
相關問題