ASP.Net 4.0 with TinyMCE and XML Encoding re-encodes Content on Postback

匆匆过客 提交于 2019-12-04 06:55:27

Could it be that the data is being reloaded into the tinymce window after it is saved?

When I encountered this before in TinyMCE/WebForms, it was easily fixed by decoding the data before populating the form field and after postbacks:

TextAreaID.Text = Server.HtmlDecode("<p>hello</p>");

I have just had a similar problem with Tinymce and Asp.NET MVC. In my case what was happening was:

  1. The form is submitted and tinymce html encodes the content (I am using the encoding: 'xml' option)
  2. In my server side post action, I html decode the tags I want to allow (simplified example: decodedHtml = model.HtmlContent.Replace("&lt;p&gt;", "<p>")). Then after saving to the database and etc, I update model.HtmlContent with the decoded html (model.HtmlContent = decodedHtml)

but at this point the tinymce editor was showing the encoded html, i.e. &lt;p&gt;test&lt;/p&gt; instead of <p>test</p> , even though I did model.HtmlContent = decodedHtml in my post action. What actually happens is asp.net ignores the value in the model on postback and instead binds the posted value (see here http://weblog.west-wind.com/posts/2012/Apr/20/ASPNET-MVC-Postbacks-and-HtmlHelper-Controls-ignoring-Model-Changes for more details on how this works).

A way around this is in your post action to do

ModelState.Remove("HtmlContent");

and then it will bind the value in your view model instead of the posted value.

So in my case, the issue wasn't actually with tinymce but with the way form posts work in asp.net mvc. Hope this helps someone.

Does a look at the entity_encoding setting help?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!