Example of API in ASP.NET MVC Rich Text Editor Control
This sample demonstrates the usage of API in Rich Text Editor. Use the properties panel to change the maximum length, read only mode, disable status, to get value, enable HTML encode, select all content and get selected text from the Rich Text Editor.
Properties
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "apiEditor",
ShowCharCount = true,
MaxLength = 1000,
Value = (string)ViewData["RTEValue"]
})
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "apiEditor",
ShowCharCount = true,
MaxLength = 1000,
Value = (string)ViewData["RTEValue"]
})
<div class="demo-property-panel">
<h3>Properties</h3>
<div class="demo-property-row"><label for="maxlength">Max Length</label><input id="maxlength" type="number" min="555" max="2000" value="1000" placeholder="Maximum Length" /></div>
<div class="demo-property-row"><label for="readonly">Readonly</label><input id="readonly" type="checkbox" /></div>
<div class="demo-property-row"><label for="enable">Enable</label><input id="enable" type="checkbox" checked /></div>
<div class="demo-property-row"><label for="enablehtml">Enable HTML Encode</label><input id="enablehtml" type="checkbox" /></div>
<div class="demo-property-row" id="apiButtons">
<button type="button" id="getVal" class="btn btn-outline-secondary btn-sm">Get Value</button>
<button type="button" id="selectHtml" class="btn btn-outline-secondary btn-sm">Get Selection</button>
<button type="button" id="selectAll" class="btn btn-outline-secondary btn-sm">Select All</button>
</div>
</div>
<script>
var rteObj = function () { return window.Lumen.get('apiEditor'); };
document.getElementById('maxlength').addEventListener('change', function (e) { rteObj().setSettings({ maxLength: e.target.value }); });
document.getElementById('readonly').addEventListener('change', function (e) { rteObj().setReadOnly(e.target.checked); });
document.getElementById('enable').addEventListener('change', function (e) { if (e.target.checked) { rteObj().enable(); } else { rteObj().disable(); } });
document.getElementById('enablehtml').addEventListener('change', function (e) { rteObj().setSettings({ enableHtmlEncode: e.target.checked }); });
// The buttons keep the editor's selection (a button takes the focus on a mouse press).
document.getElementById('apiButtons').addEventListener('mousedown', function (e) { e.preventDefault(); });
document.getElementById('getVal').addEventListener('click', function () { alert(rteObj().value); });
document.getElementById('selectHtml').addEventListener('click', function () { alert(rteObj().getSelection()); });
document.getElementById('selectAll').addEventListener('click', function () { rteObj().selectAll(); });
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class RichTextEditorController : Controller
{
// The page is Views/RichTextEditor/Api.cshtml; it draws what ViewData holds.
public ActionResult Api()
{
ViewData["RTEValue"] = "<p>Rich Text Editor is a WYSIWYG editing control that will reduce the effort for users while trying to express their formatting word content as HTML or Markdown format.</p>" +
"<p><b>API's:</b></p><ul><li><p>maxLength - allows restricting the maximum length to be entered.</p></li><li><p>readOnly - allows to change it as a non-editable state.</p></li>" +
"<li><p>enabled - enable or disable the RTE component.</p></li><li><p>enableHtmlEncode - Get the encoded string value through value property and source code panel.</p></li>" +
"<li><p>getValue - get the value of RTE.</p></li><li><p>getSelection - get the selected text of RTE.</p></li><li><p>selectAll - select all content in RTE.</p></li></ul>";
return View();
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
In this demo, we are going to ensure the API’s behaviour by
- Changing the value of the
MaxLengthtext box to change the maximum length of characters (setSettings({ maxLength })). - Clicking the
ReadOnlycheck box to enable or disable the editable and non-editable mode of the editor (setReadOnly()). - Clicking the
Enabledcheck box to enable or disable the editor (enable(),disable()). - Clicking the
EnableHtmlEncodecheck box to get the value encoded (<p>) in the text area that is posted (setSettings({ enableHtmlEncode })). - Clicking the
getValuebutton, which shows the editor’s value in the alert window. - Clicking the
getSelectionbutton, which shows the selected text in the alert window. - Clicking the
selectAllbutton, which selects all the text content in the editor.