josefpospisil0
josefpospisil0

Reputation: 155

CKEditor on ASP.NET MVC - how to edit a document

I have found a lot of tutorials on how to setup CKEditor on ASP.NET MVC, but they were mostly focused on how to create documents. I need not only to create, but to edit documents as well.

I use a basic razor action to create a textarea,

@Html.TextAreaFor(model => model.Description)

and basic javascript to setup the editor.

CKEDITOR.replace('Description', {
        language: 'cs'
    });

However, my editor loads blank. Always, even if model.Description contains already some text. Add the setData function only made the Rich text editor disappear and loaded the textarea with HTML (escaped, as I checked in the source). My questions are:

  1. How can I set the text to edit in the editor?
  2. Isn't there a more effective way or e better editor to handle this?

Upvotes: 0

Views: 2173

Answers (1)

Joel Peltonen
Joel Peltonen

Reputation: 13402

You don't need #. Do you use jquery? Try this:

$(document).ready(function() {
    // Get the description element to check it exists and that it has a value
    var d = $('#Description');
    alert(d.val(););

    // Create the editor
    CKEDITOR.replace('Description', {
        language: 'en'
    });
});

If you don't use jquery, try this:

function replaceCke(){
    // Get the description element to check it exists and that it has a value
    var d = document.getElementById("Description");
    alert(d.value);

    // Create the editor
    CKEDITOR.replace('Description', {
        language: 'en'
    });
}

if(window.addEventListener) {
    window.addEventListener('load', replaceCke, false);
} else {
    window.attachEvent('onload', replaceCke);
}

Upvotes: 1

Related Questions