简体   繁体   English

CK 编辑器在回发时更改内容 (4.10.1)

[英]CK Editor altering content on postback (4.10.1)

I'm having an issue with CKEditor encoding it's html content when the model state is not valid.当模型状态无效时,CKEditor 编码它的 html 内容时遇到问题。 For example if I submit "Hello World" it hits the server successfully as an encoded html string <p>Hello World</p>例如,如果我提交“Hello World”,它会作为编码的 html 字符串<p>Hello World</p>成功命中服务器<p>Hello World</p> . .

If I want to load content I pass a decoded html string as the value to the textarea and it loads fine <p>Hello World</p> .如果我想加载内容,我会将一个解码的 html 字符串作为值传递给 textarea 并且它加载得很好<p>Hello World</p> However if the page posts back, eg when (!ModelState.IsValid) then it renders incorrectly as:但是,如果页面回发,例如 when (!ModelState.IsValid)那么它会错误地呈现为:

<p>&lt;p&gt;Hello World&lt;/p&gt;</p>

Here's the kicker, it still receives <p>Hello World</p> as the field value on the postback.这是踢球者,它仍然接收<p>Hello World</p>作为回发的字段值。 What gives?是什么赋予了?

I'm using the following source:我正在使用以下来源:

const ckEditorSrc = "//cdn.ckeditor.com/4.10.1/standard/ckeditor.js";

and the bellow initialiser:和波纹管初始化程序:

CKEDITOR.replace('js-ck-editor', { htmlEncodeOutput: true});
CKEDITOR.config.height = 600;

I've checked and the initializer is hit after postback.我已经检查过并且在回发后初始化程序被命中。

The only other bit of information that may help is that the source is loaded dynamically using a simple loadScript function.唯一可能有帮助的其他信息是使用简单的 loadScript 函数动态加载源。

function loadScript(url, callback) {// loads a script onto a page
    var script = document.createElement("script");
    script.type = "text/javascript";
    if (script.readyState) {  //IE
        script.onreadystatechange = function () {
            if (script.readyState === "loaded" || script.readyState === "complete") {
                script.onreadystatechange = null;
                callback();
            }
        };
    } else {  //Others
        script.onload = function () {
            callback();
        };
    }

    script.src = url;
    document.getElementsByTagName("head")[0].appendChild(script);
}

Here's the full init function for brevity:为简洁起见,这是完整的 init 函数:

var ckEditor = function () {// init ckeditor

    if ($('#js-ck-editor').length) {
        const ckEditorSrc = "//cdn.ckeditor.com/4.10.1/standard/ckeditor.js";
        loadScript(ckEditorSrc, function () {

            CKEDITOR.replace('js-ck-editor', { htmlEncodeOutput: true});
            CKEDITOR.config.height = 600;
        }
    }
}

When doing postback you will have ModelState filled.在进行回发时,您将填充 ModelState。

@Html.TextAreaFor(x => x.Text) tries to resolve this ModelState. @Html.TextAreaFor(x => x.Text)尝试解析此 ModelState。 It will resolve to encoded value, and encode it again (twice).它将解析为编码值,并再次编码(两次)。

You could call ModelState.Clear() in Post action before returning view您可以在返回视图之前在 Post 操作中调用ModelState.Clear()

So it turns out the issue was with using asp.net html helpers.所以事实证明问题在于使用 asp.net html helpers。

I changed @Html.TextAreaFor(m=>m.Text)我改变了@Html.TextAreaFor(m=>m.Text)

to <textarea name="Text" id="js-ck-editor">@Model.Text</textarea><textarea name="Text" id="js-ck-editor">@Model.Text</textarea>

Now it works fine.现在它工作正常。 After hours of scrolling ckeditor documentation it turns out it was such a simple solution.经过数小时滚动 ckeditor 文档,结果证明这是一个如此简单的解决方案。 Ah well hope this helps someone else one day.啊,希望有一天这能帮助别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM