Save and Load Bootstrap Wysiwyg editor content

Billa picture Billa · Jun 1, 2013 · Viewed 20.4k times · Source

I am using Bootstrap Wysiwyg Editor. It looks great and adding my text and inserted image into the content area.

All i need is i want to save the content into database and again when user loads the template name from dropdown i need to fill the editor from the database.

I tried $('#editor').html() but not sure how can i send this data to server

Note: Using ASP.Net MVC

Answer

Fusor picture Fusor · Jul 2, 2013

(I don't know if it is the best way, but it's working)

Consider a simple model :

public class Contact
{
    public string Notes { get; set; }
}

I'm using a simple jquery script in order to get the content of the wysiwyg and put it into a hidden input.

I do this automatically when the user clicks on the "Save" button.

Here's the JS :

<script language=javascript>

$(function () {
    $('#NotesWysiwyg').wysiwyg();
    $('#btnSave').bind('click', function () {
        $("#Notes").val($("#NotesWysiwyg").html().replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;'));
    });
});

The HTML content :

<div id="NotesWysiwyg"style="height:166px; width:395px; border:1px solid lightgray;display:table-cell"></div>

@Html.HiddenFor(contact => contact.Notes)

<button id="btnSave" type="submit">Save</button>

On the server side :

// Workaround : Encode WYSIWYG HTML
if (model.Notes != null)
     model.Notes = WebUtility.HtmlDecode(model.Notes);