Example of @ Mention in ASP.NET MVC Markdown Editor Control
This example shows how to integrate mentions within the Markdown Editor. Type the @ character and select a user from the suggestion list.
@* The editor's settings are in the view; the data they read (ViewData) is set in the controller action (see the controller tab). *@
@Html.UiMarkdownEditor(new UiMarkdownEditorOptions
{
Id = "mentionIntegration",
Height = "250px",
Placeholder = "Enter your text here...",
Value = (string)ViewData["MDValue"],
ToolbarSettings = new UiMarkdownToolbar { Items = (List<string>)ViewData["Tools"] },
Mention = new UiMarkdownMention { SortOrder = UiDropDownSortOrder.Ascending, ShowAvatar = true, InsertTemplate = "[${Text}](mailto:${Subtext})", NoRecordsText = "No item related to the search", PopupWidth = "250px", PopupHeight = "200px", Items = (List<UiMarkdownMentionItem>)ViewData["EmailData"] }
})
@Html.UiMarkdownEditor(new UiMarkdownEditorOptions
{
Id = "mentionIntegration",
Height = "250px",
Placeholder = "Enter your text here...",
Value = (string)ViewData["MDValue"],
ToolbarSettings = new UiMarkdownToolbar { Items = (List<string>)ViewData["Tools"] },
Mention = new UiMarkdownMention { SortOrder = UiDropDownSortOrder.Ascending, ShowAvatar = true, InsertTemplate = "[${Text}](mailto:${Subtext})", NoRecordsText = "No item related to the search", PopupWidth = "250px", PopupHeight = "200px", Items = (List<UiMarkdownMentionItem>)ViewData["EmailData"] }
})
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class MarkdownEditorController : Controller
{
// The page is Views/MarkdownEditor/Mention.cshtml; it draws what ViewData holds.
public ActionResult Mention()
{
ViewData["MDValue"] = "Hello [Albert](mailto:albert@gmail.com)\n\nWelcome to the mention integration with markdown editor demo. Type @ character and tag user from the suggestion list.";
ViewData["Tools"] = new List<string>
{
"Bold", "Italic", "StrikeThrough", "|", "Formats", "Blockquote", "OrderedList", "UnorderedList", "Superscript", "Subscript", "|", "CreateTable",
"CreateLink", "Image", "|", "Preview", "|", "Undo", "Redo"
};
ViewData["EmailData"] = new List<UiMarkdownMentionItem>
{
new UiMarkdownMentionItem { Text = "Alice Johnson", Subtext = "alice@example.com" },
new UiMarkdownMentionItem { Text = "Alan Chu", Subtext = "alan@example.com" },
new UiMarkdownMentionItem { Text = "Bob Martin", Subtext = "bob@example.com" },
new UiMarkdownMentionItem { Text = "Carol Danvers", Subtext = "carol@example.com" },
new UiMarkdownMentionItem { Text = "Dan Brown", Subtext = "dan@example.com" },
new UiMarkdownMentionItem { Text = "Erin Hall", Subtext = "erin@example.com" }
};
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>
The @ mention displays a list of items that users can select or tag from the suggested list. In this demo, the following properties are configured with the popup dimensions:
SortOrder– Sorts the suggestion list.ShowAvatar– Shows the first letter of each item in a circle, the avatar, in the suggestion list.InsertTemplate– Sets what is written for the chosen item: here a Markdown link with the person’s e-mail address.PopupWidthandPopupHeight– Set the dimensions of the suggestion list.NoRecordsText– Sets the text shown when nothing matches.