Example of Overview in ASP.NET MVC Rich Text Editor Control
This sample demonstrates the full features of the ASP.NET MVC Rich Text Editor that includes all the tools and functionalities.
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "overviewEditor",
Name = "Overview",
ShowCharCount = true,
EnableXhtml = true,
EnableTabKey = true,
ToolbarSettings = new UiRichTextToolbar { Items = (List<string>)ViewData["Tools"] },
QuickToolbarSettings = new UiRichTextQuickToolbar { ShowOnRightClick = true, Text = (List<string>)ViewData["Text"], Table = (List<string>)ViewData["Table"] },
SlashMenuSettings = new UiRichTextSlashMenuSettings { Enable = true },
ImportWord = new UiRichTextImportSettings { ServiceUrl = Url.Action("ImportWord") },
ExportWord = new UiRichTextExportSettings { ServiceUrl = Url.Action("ExportWord"), FileName = "RichTextEditor" },
ExportPdf = new UiRichTextExportSettings { ServiceUrl = Url.Action("ExportPdf"), FileName = "RichTextEditor" },
Value = (string)ViewData["RTEValue"]
})
@Html.UiMention(new UiMentionOptions
{
Id = "overviewMention",
Target = "overviewEditor_content",
ShowMentionChar = true,
Highlight = true,
PopupWidth = "240px",
Items = new[]
{
new UiListItem("maria@example.com", "Maria Santos"), new UiListItem("ken@example.com", "Ken Ito"), new UiListItem("ana@example.com", "Ana Ruiz"),
new UiListItem("bob@example.com", "Bob Martin"), new UiListItem("lena@example.com", "Lena Fischer"), new UiListItem("omar@example.com", "Omar Haddad")
}
})
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "overviewEditor",
Name = "Overview",
ShowCharCount = true,
EnableXhtml = true,
EnableTabKey = true,
ToolbarSettings = new UiRichTextToolbar { Items = (List<string>)ViewData["Tools"] },
QuickToolbarSettings = new UiRichTextQuickToolbar { ShowOnRightClick = true, Text = (List<string>)ViewData["Text"], Table = (List<string>)ViewData["Table"] },
SlashMenuSettings = new UiRichTextSlashMenuSettings { Enable = true },
ImportWord = new UiRichTextImportSettings { ServiceUrl = Url.Action("ImportWord") },
ExportWord = new UiRichTextExportSettings { ServiceUrl = Url.Action("ExportWord"), FileName = "RichTextEditor" },
ExportPdf = new UiRichTextExportSettings { ServiceUrl = Url.Action("ExportPdf"), FileName = "RichTextEditor" },
Value = (string)ViewData["RTEValue"]
})
@Html.UiMention(new UiMentionOptions
{
Id = "overviewMention",
Target = "overviewEditor_content",
ShowMentionChar = true,
Highlight = true,
PopupWidth = "240px",
Items = new[]
{
new UiListItem("maria@example.com", "Maria Santos"), new UiListItem("ken@example.com", "Ken Ito"), new UiListItem("ana@example.com", "Ana Ruiz"),
new UiListItem("bob@example.com", "Bob Martin"), new UiListItem("lena@example.com", "Lena Fischer"), new UiListItem("omar@example.com", "Omar Haddad")
}
})
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.IO;
using System.Web;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
using Newtonsoft.Json;
namespace YourApp.Controllers
{
public class RichTextEditorController : Controller
{
// The page is Views/RichTextEditor/Overview.cshtml; it draws what ViewData holds.
// The files under ~/Content are the demo's own sample files: put yours at those paths, or change the addresses.
public ActionResult Overview()
{
ViewData["Tools"] = new List<string>
{
"Undo", "Redo", "|", "ImportWord", "ExportWord", "ExportPdf", "|",
"Bold", "Italic", "Underline", "StrikeThrough", "InlineCode", "|", "CreateLink", "Image", "CreateTable", "CodeBlock",
"HorizontalLine", "Blockquote", "|", "LineHeight", "Formats", "Alignments", "|", "BulletFormatList", "NumberFormatList", "Checklist", "|", "Outdent", "Indent", "|",
"FontColor", "BackgroundColor", "FontName", "FontSize", "|", "LowerCase", "UpperCase", "|", "SuperScript", "SubScript", "|",
"EmojiPicker", "Video", "Audio", "|", "FormatPainter", "ClearFormat", "|", "Print", "FullScreen", "|", "SourceCode"
};
ViewData["Text"] = new List<string> { "Formats", "|", "Bold", "Italic", "FontColor", "BackgroundColor", "|", "CreateLink", "Image", "CreateTable", "Blockquote", "|", "UnorderedList", "OrderedList", "Indent", "Outdent" };
ViewData["Table"] = new List<string> { "TableRows", "TableColumns", "TableRemove" };
ViewData["RTEValue"] = "<h2>Welcome to the Rich Text Editor</h2><p>The 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>" +
"<h3>Do you know the key features of the editor?</h3><ul><li>Basic features include headings, block quotes, numbered lists, bullet lists, and support to insert images, tables, audio, and video.</li>" +
"<li>Inline styles include <b>bold</b>, <em>italic</em>, <u>underline</u>, <s>strikethrough</s>, <a href=\"https://example.com/guide\">hyperlinks</a>, <code>InlineCode</code>, 😀 and more.</li>" +
"<li>The toolbar has multi-row, expandable, and scrollable modes. The Editor supports an inline toolbar, a floating toolbar, and custom toolbar items.</li>" +
"<li>Integration with the Mention control lets users tag other users. Type <b>@</b> to try it.</li>" +
"<li><b>Paste from MS Word</b> - helps to reduce the effort while converting the Microsoft Word content to HTML format with format and styles.</li>" +
"<li>Other features: placeholder text, character count, form validation, enter key configuration, resizable editor, IFrame rendering, tooltip, source code view, RTL mode, persistence, HTML Sanitizer, autosave, and more.</li></ul>" +
"<h3>Elevating Your Content with Images</h3><p>Images can be added to the editor by pasting or dragging into the editing area, or using the toolbar to insert one as a URL or upload a file. Configure <code>InsertImageSettings</code> to upload, save, or remove them on the server.</p>" +
"<p><img alt=\"The Lumen logo\" src=\"" + Url.Content("~/Content/images/LumenLogo.png") + "\" width=\"300\"></p>" +
"<blockquote><p><em>Easily access Audio, Image, Link, Video, and Table operations through the quick toolbar by right-clicking on the corresponding element with your mouse.</em></p></blockquote>";
return View();
}
/// <summary>The Image sample's uploads: kept in memory (the 50 newest), as a demo needs no storage of its own.</summary>
private static readonly ConcurrentDictionary<string, KeyValuePair<DateTime, byte[]>> Images = new ConcurrentDictionary<string, KeyValuePair<DateTime, byte[]>>();
/// <summary>The Export sample's Word action (ExportWord.ServiceUrl): JSON { html, fileName } in, a .docx out.</summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult ExportWord()
{
ExportRequest request = ReadExport();
if (request == null) { return new HttpStatusCodeResult(400, "No HTML was sent."); }
byte[] file;
try { file = UiRichTextWord.Export(request.Html, request.Stylesheet); }
catch (ArgumentException) { return new HttpStatusCodeResult(400, "The HTML is larger than 5 MB."); }
return File(file, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", FileName(request.FileName) + ".docx");
}
/// <summary>The Export sample's PDF action (ExportPdf.ServiceUrl): JSON { html, fileName } in, a PDF out.</summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult ExportPdf()
{
ExportRequest request = ReadExport();
if (request == null) { return new HttpStatusCodeResult(400, "No HTML was sent."); }
byte[] file;
try { file = UiRichTextPdf.Export(request.Html, request.FileName, request.Stylesheet); }
catch (ArgumentException) { return new HttpStatusCodeResult(400, "The HTML is larger than 5 MB."); }
return File(file, "application/pdf", FileName(request.FileName) + ".pdf");
}
/// <summary>
/// The Import sample's action (ImportWord.ServiceUrl): a .docx in (multipart, field "file"), { html } out, made safe; or 400 with
/// { error } for a file that is too large, damaged, not a .docx, or unsafe to open.
/// </summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult ImportWord(HttpPostedFileBase file)
{
string html, error;
if (file == null || file.ContentLength <= 0) { error = "No file was sent."; }
else if (UiRichTextWord.TryImport(file.InputStream, out html, out error))
{
return Json(new { html });
}
Response.StatusCode = 400;
Response.TrySkipIisCustomErrors = true;
return Json(new { error });
}
private ExportRequest ReadExport()
{
string body;
Request.InputStream.Position = 0;
using (var reader = new StreamReader(Request.InputStream)) { body = reader.ReadToEnd(); }
var request = JsonConvert.DeserializeObject<ExportRequest>(body);
return request == null || request.Html == null ? null : request;
}
private static string FileName(string name)
{
string safe = new string((name ?? string.Empty).Where(c => char.IsLetterOrDigit(c) || c == ' ' || c == '-' || c == '_').ToArray()).Trim();
return safe.Length == 0 ? "Document" : (safe.Length > 80 ? safe.Substring(0, 80) : safe);
}
private sealed class ExportRequest
{
public string Html { get; set; }
public string FileName { get; set; }
/// <summary>Syncfusion's export stylesheet (Phase 5b), when the editor has one.</summary>
public string Stylesheet { get; set; }
}
}
}
@* 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>
@* Rich Text Editors with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
The quick toolbar provides a convenient way to customize Image, Video, Audio, Table, and Link elements. Simply right-click on the desired element, utilizing the ShowOnRightClick property, and the quick toolbar will appear, providing an easy way for customization.
The editor's toolbar includes tools for formatting content. The toolbar consists of:
Lists- NumberFormat list and BulletFormat list types.Links- A hyperlink can be inserted into the editor for quick access to related information.Image- Inserts and manages images.Table- Inserts and manages Tables.Alignment- Aligns the content with left, center, and right margins.Line Height- Adjusts the vertical spacing between lines of text.Undo/Redo- Allows undo/redo operations.Indent/ Outdent- Increases/decreases the indent level of the content.Font- Able to do styling on text like font family, size, fore color, and background color.Lower / Upper case– Changes the casing of the selected text.Subscript / Superscript- Makes the selected text as subscript (lower)/superscript(upper).FullScreen- Stretches the editor to the maximum width and height of the browser window.Format– Formats the sentence in different ways such as heading level, quotation, and code snippet.Styles– Allows you to apply inline styles to the selected content like bold, italic, and more.Insert Emoticon- Inserts the emoticon to the editorAudio- Inserts and manages audios.Video- Inserts and manages videos.Format Painter- The Format Painter feature allows you to copy the formats and apply them to content without formatting thus saving time to reformat the content.Slash Menu- The Slash Menu feature lets users apply formats, open dialogs by typing "/" in the editor.Import / Export- The Import/Export feature enables users to import content from Word documents into the editor and export the editor's content into Word and PDF files.Inline Code- Formats selected text as inline code, highlighting code snippets within the text.Code Block- The Code Block feature allows you to insert and display blocks of code with preserved formatting, making it ideal for sharing code snippets clearly and accurately.Horizontal Line- A horizontal line can be inserted into the editor to visually separate sections of content.Checklist- Creates interactive lists with checkable items that users can mark as complete or incomplete.