简体   繁体   English

MVC 5 CKEditor SetData从数据库到编辑器

[英]MVC 5 CKEditor SetData to editor from DB

Almost got everything working with CKEditor plugin and the only problem left is to load the data already saved to the DB into the WYSIWYG editor. 几乎可以使用CKEditor插件进行所有操作,剩下的唯一问题是将已经保存到数据库的数据加载到WYSIWYG编辑器中。

If i try something like this in javascript as a test it works fine. 如果我在javascript中尝试类似的方法作为测试,则效果很好。

<script>
    var value = "<h1>Awesome</h1>";

    var config = {};
    editor = CKEDITOR.appendTo('editor', config, html);

    editor.setData(value);
</script>

So this creates the editor and actually tries to load the "html" variable into it but this is currently just empty. 因此,这将创建编辑器,并实际上尝试将“ html”变量加载到该编辑器中,但是当前它只是空的。

So if I look in my DB I have a field called Description (nvarchar(max),null) which currently has this value: <h1>Awesome</h1> 因此,如果我在数据库中查看,则有一个名为Description(nvarchar(max),null)的字段,该字段当前具有以下值: <h1>Awesome</h1>

But if I try to get this value in Javascript I get an error: 但是,如果我尝试在Javascript中获取此值,则会收到错误消息:

Uncaught SyntaxError: Unexpected token ILLEGAL 

And if i look at the console log it looks like this: 如果我查看控制台日志,它看起来像这样:

 var value = '<h1>Awesome</h1>
Uncaught SyntaxError: Unexpected token ILLEGAL 
';

UPDATE 更新

Ok, after some experimenting I managed to create a working solution. 好的,经过一些试验,我设法创建了一个可行的解决方案。

I created a hidden field for the Description 我为说明创建了一个隐藏字段

@Html.HiddenFor(m => m.Note.Description)

And I am showing it like this: 我正在这样显示它:

<div id="editorcontents" class="well padding-10">
  @Html.Raw(Model.Note.Description)
</div>

And when I go into edit mode I hide the editor and set the value to the editor from the hidden field. 当我进入编辑模式时,我隐藏了编辑器,并从隐藏字段中将值设置为编辑器。

if (editor)
    return;

$('#editorcontents').hide();
var config = {};
var html = $('#Note_Description').val();

editor = CKEDITOR.appendTo('editor', config, html);

And in the success method of my ajax update function I destroy the editor and update the value in the hidden field. 在我的ajax更新功能的成功方法中,我销毁了编辑器并更新了隐藏字段中的值。

success: function (data) {
   editor.destroy();
   editor = null;
   $('#Note_Description').val(data[2].Description);
}

So somehow getting the value with Jquery fixed the problem. 因此,以某种方式通过Jquery获取值解决了该问题。

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

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