1. 程式人生 > >在asp.net mvc中使用百度UEditor初始化內容遇到自資料庫中讀取的html字串在UEditor中始終是html編碼的問題

在asp.net mvc中使用百度UEditor初始化內容遇到自資料庫中讀取的html字串在UEditor中始終是html編碼的問題

<script id="Content" name="Content" type="text/plain" style="width: 650px; height: 500px;">
        @Model.Content
</script>

這個應該在UEditor被顯示為文件形式而不是html原始碼。剛開始以為是UEditor哪個地方設定不對。於是進行了嘗試:

UE.getEditor('Content').ready(function(){//this是當前建立的編輯器例項this.setContent('@Model.Content')})
問題依舊。

採用UE.getEditor('Content',{initialContent:'@Model.Content'}),仍是同樣的問題,網上進行了各種搜尋,似乎沒人和我遇到的問題相似,始終找不到答案。

後來我把資料庫中的content欄位中的內容直接複製出來然後寫入

<script id="Content" name="Content" type="text/plain" style="width: 650px; height: 500px;">
        <p style="margin: 12px 0px; padding: 0px; font-family: 微軟雅黑; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28.7999992370605px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">和中華民族傳統佳節中秋節即將到來。今天上午,自治區2016年古爾邦節茶話會在新疆迎賓館舉行,各族各界代表歡聚一堂,共慶佳節。自治區黨委書記、新疆生產建設兵團黨委第一書記、第一政委陳全國發表了熱情洋溢的講話,代表自治區黨委、政府,向全區各族穆斯林群眾致以良好的祝願,向全區各族幹部群眾和所有關心支援新疆工作的各界人士致以美好的祝福。</p><p style="margin: 12px 0px; padding: 0px; font-family: 微軟雅黑; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28.7999992370605px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">   一杯清茶香、共話團結情。今天的新疆迎賓館會議中心暖意濃濃,到處洋溢著歡樂祥和的節日氣氛。</p><p style="margin: 12px 0px; padding: 0px; font-family: 微軟雅黑; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28.7999992370605px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">   茶話會前,陳全國等自治區領導與各族各界代表親切握手、互致祝福。</p><p style="margin: 12px 0px; padding: 0px; font-family: 微軟雅黑; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28.7999992370605px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">   自治區黨委副書記、自治區主席雪克來提 扎克爾主持茶話會。自治區和兵團領導乃依木 亞森、努爾蘭阿不都滿金、孫金龍、朱海侖、黃衛、肖開提 依明、熊選國、徐海榮、哈尼巴提 沙布開、李學軍、馬學軍、彭家瑞、劉新齊等出席茶話會。</p><p><br/></p>
</script>

這次正確顯示出來了。看來找問題的方向錯了。不是UEdior的問題,而是mvc在頁面中顯示的問題。

先後試用了:

@Model.Content呈現,無效

@Html.DisplayFor(m=>m.Content)無效
@Html.ValueFor(m=>m.Content)無效
各種嘗試,沒一個起作用的
甚至建立了一隱藏欄位,在資料庫中讀取Content的內容賦值,通過檢視原始碼,<都被自動編碼為:&lt;  。  >都被編碼為:&gt;

這MVC真是蛋疼。後來終於搜尋到一個合適的:

<script id="Content" name="Content"
 type="text/plain" style="width650pxheight500px;">     @Html.Raw(Model.Content) </script>
這個沒問題了。終於解決了此問題。記錄備查