2016-07-06 72 views
0

我有問題,我想要做的是:ASP.NET的剃刀:渲染ViewBag項目和替換字符

我從我的控制器ViewBag.Email變量獲取用戶的電子郵件地址。

我想要的標籤,這將有ID名爲「令牌」 +用戶電子郵件代替字符「@」到「_」

(即電子郵件是[email protected],我想有標籤爲id:'tokenabc_abc.com')

<label id="@Html.Raw("token" + ViewBag.Email.replace("@", "_"))"> 
    @ViewBag.Tokens 
</label> 

我試過用這段代碼,但是我有問題。

錯誤:

An exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException'   
occurred in System.Core.dll but was not handled in user code 

Additional information: 'string' does not contain a definition for 'replace' 
+0

什麼問題?如果您在控制器中生成'格式化'文本並將其傳遞到視圖將會好得多。 –

+0

你有什麼問題?當你這樣做時,你會得到什麼標識? –

+0

我用錯誤代碼編輯了我的問題。順便說一下,我已經成功地遵循斯蒂芬的建議,在控制器中生成字段,現在可以。 – luka032

回答

0

的問題是在寫方法資本R替換 - >替換。

<label id="@Html.Raw("token" + ViewBag.Email.Replace("@", "_"))"> 
    @ViewBag.Tokens 
</label> 
1

,你可以寫「替換」,你會得到工作的變體。這是代碼的工作

<label id="@Html.Raw("token" + ViewBag.Email.Replace("@", "_"))"> 
@ViewBag.Tokens