2015-08-03 22 views
0

我是mvc的初學者。當我將項目升級到MVC6時,我遇到了一些問題,也無法找到解決方案。我的代碼塊是:如何在MVC6中使用HtmlTextWritter

public static void WriteTo(HtmlHelper helper, string value) 
    { 
     var baseWriter = helper.ViewContext.Writer; 
     using (HtmlTextWriter textWriter = new HtmlTextWriter(baseWriter)) 
     { 
      textWriter.AddAttribute(HtmlTextWriterAttribute.Type, "text/javascript"); 
      textWriter.RenderBeginTag(HtmlTextWriterTag.Script); 
      textWriter.Write(value); 
      textWriter.RenderEndTag(); 
     } 
    } 

如果有任何的方式來實現MVC6相同的情況下(如使用HtmlTextWritter的),或者如果在我的代碼任何差錯block.can請你提出解決方案。即使用谷歌嘗試,但我不能得到選擇解決方案。

+0

@thanks CodeCaster:任何替代方法來實現這種情況? – Riowan

回答

0

的MVC等效該陳舊的格式(HtmlTextWriter的爲的WebForms)是這樣的:

<script type="text/javascript">@value</script> 

value是一個變量,它是存在於您的視圖。

+0

「值」已從另一個方法傳遞「WriteTo(helper,string.Format(content));」在這種方法中,只有我已經做了htmltextwriter操作。 – Riowan