Example of Template Functionalities in ASP.NET MVC Mention Control
This sample demonstrates the template functionalities of the Mention component. Type the @@ character in the editable element and select or tag the user from the customized suggestion list.
${Name} ${EmailId}
@Html.UiMention(new UiMentionOptions
{
Id = "template",
Target = "templateMention",
MentionChar = "@",
AllowSpaces = true,
ItemTemplate = "#mentionItem",
NoRecordsText = "No item related to the search",
DisplayTemplate = "#mentionDisplay",
PopupWidth = "250px",
PopupHeight = "200px",
DataSource = (IEnumerable<DropdownsDemoController.SportsData>)ViewData["sports"],
Fields = new UiDropDownFields { Text = "Name" }
})
<style>
#mention-controls { margin: 0 auto; width: 400px; padding-top: 30px; }
#mention-controls label { display: block; font-weight: 600; margin-bottom: 6px; }
#mention-controls .dropdown-sample-target { min-height: 120px; border: 1px solid #ced4da; border-radius: 4px; padding: 8px; }
.listItems { display: flex; align-items: center; gap: 8px; }
.mentionEmpImage { width: 32px; height: 32px; border-radius: 50%; }
.listItems .person { font-weight: 600; }
.listItems .email { font-size: 12px; opacity: .7; margin-left: auto; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
<template id="mentionItem"><div class="listItems"><img class="mentionEmpImage" src="@Url.Content("~/Content/dropdownlist/images/Employees/")${Eimg}.png" alt="employee"/><span class="person"> ${Name} </span><span class="email">${EmailId}</span></div></template>
<template id="mentionDisplay">${Name}</template>
<div class="control-section">
<div id="mention-controls">
<label id="template-size">Compose your content</label>
<div id="templateMention" class="dropdown-sample-target" contenteditable="true" role="textbox" aria-label="Compose your content"></div>
@Html.UiMention(new UiMentionOptions
{
Id = "template",
Target = "templateMention",
MentionChar = "@",
AllowSpaces = true,
ItemTemplate = "#mentionItem",
NoRecordsText = "No item related to the search",
DisplayTemplate = "#mentionDisplay",
PopupWidth = "250px",
PopupHeight = "200px",
DataSource = (IEnumerable<DropdownsDemoController.SportsData>)ViewData["sports"],
Fields = new UiDropDownFields { Text = "Name" }
})
</div>
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class MentionController : Controller
{
// The page is Views/Mention/Template.cshtml; it draws what ViewData holds.
public ActionResult Template()
{
ViewData["sports"] = new SportsData().SportsList();
return View();
}
public class SportsData
{
public string Name { get; set; }
public string Eimg { get; set; }
public string EmailId { get; set; }
public List<SportsData> SportsList()
{
List<SportsData> mention = new List<SportsData>();
mention.Add(new SportsData { Name = "Selma Rose", Eimg = "3", EmailId = "selma@gmail.com" });
mention.Add(new SportsData { Name = "Russo Kay", Eimg = "8", EmailId = "russo@gmail.com" });
mention.Add(new SportsData { Name = "Camden Kate", Eimg = "9", EmailId = "camden@gmail.com" });
// ... 19 more rows
return mention;
}
}
}
}
@* 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 the above sample, for the template rendering the following are used
itemTemplate- It is used for displaying customized lists.displayTemplate- It is used to display, how the the value selected is previewed in the element.noRecordsTemplate- It is used to display a message if a user searches for irrelevant data in the data source bound.