Example of RichTextEditorFor in ASP.NET MVC Rich Text Editor Control
This sample demonstrates the strongly-typed extension support in Rich Text Editor control. The value of the Rich Text Editor is set from code behind and it is set to Rich Text Editor in a view page by invoking the strongly-typed RichTextEditorFor helper.
Html.UiRichTextEditorFor(m => m.Body): the field's name, value and rules come from the model. The action binds the post with
UiHtmlModelBinder, which makes the [AllowHtml] body safe before the action sees it; try a
<script> in the source view and post.
@using Lumen.UI.Mvc.Demo.Models
@model Lumen.UI.Mvc.Demo.Models.RichTextEditorArticle
@using (Html.BeginForm("Article", "RichTextEditorDemo", FormMethod.Post, new { id = "articleForm" }))
{
@Html.AntiForgeryToken()
<div class="mb-3">
@Html.UiTextBoxFor(m => m.Title, new UiTextBoxOptions { Id = "articleTitle" })
@Html.ValidationMessageFor(m => m.Title, null, new { @class = "text-danger small" })
</div>
<div class="mb-3">
@Html.UiRichTextEditorFor(m => m.Body, new UiRichTextEditorOptions { Id = "articleBody", Height = "260px" })
@Html.ValidationMessageFor(m => m.Body, null, new { @class = "text-danger small" })
</div>
<button type="submit" class="btn btn-primary" id="articleSubmit">Post the article</button>
}
@if (ViewBag.Saved == true)
{
<div class="mt-3" id="articleSaved">
<p class="mb-1"><strong>What the action received</strong> (as text):</p>
<pre class="small p-2 border rounded" id="articleSavedHtml">@Model.Body</pre>
</div>
}
@using Lumen.UI.Mvc.Demo.Models
<p>
<code>Html.UiRichTextEditorFor(m => m.Body)</code>: the field's name, value and rules come from the model. The action binds the post with
<code>UiHtmlModelBinder</code>, which makes the <code>[AllowHtml]</code> body safe before the action sees it; try a
<code><script></code> in the source view and post.
</p>
@Html.Partial("~/Views/RichTextEditorDemo/Samples/_ArticleForm.cshtml", (RichTextEditorArticle)ViewBag.Article ?? new RichTextEditorArticle
{
Title = "Quarterly update",
Body = "<p>Sales grew in <b>every</b> region this quarter.</p><ul><li>North: 12%</li><li>South: 9%</li></ul>"
})
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class RichTextEditorController : Controller
{
// The page is Views/RichTextEditor/Richtexteditorfor.cshtml; it draws what ViewData holds.
public ActionResult Richtexteditorfor()
{
return View();
}
/// <summary>
/// The RichTextEditorFor sample's form: the article as UiHtmlModelBinder binds it (the body made safe), shown back on the sample's
/// page. A body over 5 MB, or one that breaks the model's rules, comes back with its message under the field.
/// </summary>
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Article([ModelBinder(typeof(UiHtmlModelBinder))] RichTextEditorArticle article)
{
ViewBag.Article = article;
ViewBag.Saved = ModelState.IsValid;
return Sample("rte-richtexteditorfor");
}
}
}
@* 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. *@
Click the submit button to post the value in the Rich Text Editor. The validation error message will be shown below the Rich Text Editor, when posting the null value.