Code View Support in ASP.NET CORE Rich Text Editor Control
26 Aug 20257 minutes to read
The Rich Text Editor enables you to directly edit HTML code through the Source View
in the text area. Any changes you make in Source view are immediately reflected in the editor’s content, giving you greater flexibility and control over your work.
The source code is presented with enhanced formatting and consistent indentation for both block-level and inline HTML elements, resulting in a cleaner, more readable, and user-friendly editing experience.
While the visual alignment of the source code is improved, the editor’s underlying value remains unchanged.
Configuring Source Code Tool in the Toolbar
You can add the SourceCode
tool in the Rich Text Editor using the toolbarSettings
items property.
This sample used Code mirror
plugin helps to highlight the HTML content and when changes happens in code view, the same has been reflected in preview mode.
<div class="control-section">
<ejs-richtexteditor id="editor" value="@ViewBag.value" actionComplete="actionCompleteHandler" created="created">
<e-richtexteditor-toolbarsettings items="@ViewBag.tools">
</e-richtexteditor-toolbarsettings>
</ejs-richtexteditor>
</div>
<link href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.39.0/codemirror.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.3.0/codemirror.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.3.0/mode/css/css.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.3.0/mode/xml/xml.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.3.0/mode/htmlmixed/htmlmixed.js" type="text/javascript"></script>
<style>
.e-code-mirror::before {
content: '\e345';
}
.e-html-preview::before {
content: '\e350';
}
.CodeMirror-linenumber,
.CodeMirror-gutters {
display: none;
}
.sb-header {
z-index: 100;
}
</style>
<script type="text/javascript">
var defaultRTE;
var divPreview;
var myCodeMirror;
var textArea
divPreview = document.getElementById('DIV_Preview');
function created() {
defaultRTE = this;
textArea = defaultRTE.contentModule.getEditPanel();
}
function mirrorConversion(e) {
var id = defaultRTE.getID() + 'mirror-view';
var mirrorView = defaultRTE.element.querySelector('#' + id);
var charCount = defaultRTE.element.querySelector('.e-rte-character-count');
if (e.targetItem === 'Preview') {
textArea.style.display = 'block';
mirrorView.style.display = 'none';
textArea.innerHTML = myCodeMirror.getValue();
charCount.style.display = 'block';
}
else {
if (!mirrorView) {
mirrorView = ej.base.createElement('div', { className: 'e-content' });
mirrorView.id = id;
textArea.parentNode.appendChild(mirrorView);
}
else {
mirrorView.innerHTML = '';
}
textArea.style.display = 'none';
mirrorView.style.display = 'block';
renderCodeMirror(mirrorView, defaultRTE.value);
charCount.style.display = 'none';
}
}
function renderCodeMirror(mirrorView, content) {
myCodeMirror = CodeMirror(mirrorView, {
value: content,
lineNumbers: true,
mode: 'text/html',
lineWrapping: true,
});
}
function actionCompleteHandler(e) {
if (e.targetItem && (e.targetItem === 'SourceCode' || e.targetItem === 'Preview')) {
this.sourceCodeModule.getPanel().style.display = 'none';
mirrorConversion(e);
}
else {
setTimeout(function () { defaultRTE.toolbarModule.refreshToolbarOverflow(); }, 400);
}
}
</script>
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.value = @"<p>The Syncfusion Rich Text Editor, a WYSIWYG (what you see is what you get) editor, is a user interface that allows you to create, edit, and format rich text content. You can try out a demo of this editor here.</p><p><b>Key features:</b></p><ul><li><p>Provides <IFRAME> and <DIV> modes.</p></li><li><p>Bulleted and numbered lists.</p></li><li><p>Handles images, hyperlinks, videos, hyperlinks, uploads, etc.</p></li><li><p>Contains undo/redo manager. </p></li></ul><div style='display: inline-block; width: 60%; vertical-align: top; cursor: auto;'><img alt='Sky with sun' src='https://cdn.syncfusion.com/ej2/richtexteditor-resources/RTE-Overview.png' width='309' style='min-width: 10px; min-height: 10px; width: 309px; height: 174px;' class='e-rte-image e-imginline e-rte-drag-image' height='174' /></div>";
ViewBag.tools = new[] { "SourceCode" };
return View();
}
}
The Rich Text Editor provides the showSourceCode
method, which allows you to toggle programmatically between the code view and the formatted text view. When invoked, this method switches the editor’s view to the opposite state.