简体   繁体   中英

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.

If i try something like this in javascript as a test it works fine.

<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.

So if I look in my DB I have a field called Description (nvarchar(max),null) which currently has this value: <h1>Awesome</h1>

But if I try to get this value in Javascript I get an error:

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.

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

So somehow getting the value with Jquery fixed the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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