controller:
StringBuilder s = new StringBuilder(); string a = "<script>alert('我StringBuilder')</script>文檔內容"; s.AppendFormat("<h1>{0}</h1><h1>測試</h1>", a); string t=s.ToString(); ViewBag.newMes = t;
cshtml:
<div id="haha"></div> <script type="text/javascript"> $(document).ready(function () { var newMes = '@Ajax.JavaScriptStringEncode(ViewBag.newMes)'; document.getElementById("haha").innerHTML =newMes; }); </script>