Example of Different Mentioned Characters in ASP.NET MVC Mention Control
This sample demonstrates the different mentioned characters that are used to render the suggestion list. Type the @@ or # or $ or % characters to select or tag the name from respective suggestion lists. The RequireLeadingSpace property in Mention controls whether a space is needed before triggering the Mention suggestion popup. When set to false, it activates without a space; when set to true, a space is required before the Mention character. To see this feature in action, start typing with #.
@Html.UiMention(new UiMentionOptions
{
Id = "employee",
Target = "multipleList",
MentionChar = "@",
PopupWidth = "250px",
PopupHeight = "215px",
SuggestionCount = 15,
DataUrl = Url.Action("EmployeeData", "DropdownsDemo"),
Fields = new UiDropDownFields { Text = "FirstName", Value = "EmployeeID" }
})
@Html.UiMention(new UiMentionOptions
{
Id = "project",
Target = "multipleList",
MentionChar = "#",
AllowSpaces = true,
DisplayTemplate = "#displayProject",
RequireLeadingSpace = false,
DataSource = (IEnumerable<DropdownsDemoController.ProjectsData>)ViewData["project"],
Fields = new UiDropDownFields { Text = "Value" }
})
@Html.UiMention(new UiMentionOptions
{
Id = "cost",
Target = "multipleList",
MentionChar = "$",
AllowSpaces = true,
DisplayTemplate = "#displayCost",
DataSource = (IEnumerable<DropdownsDemoController.UseCostsData>)ViewData["cost"],
Fields = new UiDropDownFields { Text = "Value" }
})
@Html.UiMention(new UiMentionOptions
{
Id = "status",
Target = "multipleList",
MentionChar = "%",
AllowSpaces = true,
DisplayTemplate = "#displayStatus",
DataSource = (IEnumerable<DropdownsDemoController.StatusData>)ViewData["status"],
Fields = new UiDropDownFields { Text = "Value" }
})
<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; }
.mention-success { color: #2e7d32; }
.mention-error { color: #c62828; }
.mention-warning { color: #ef6c00; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
<template id="displayProject"><span class="mention-success">${Value}</span></template>
<template id="displayCost"><span class="mention-error">${Value}</span></template>
<template id="displayStatus"><span class="mention-warning">${Value}</span></template>
<div class="control-section">
<div id="mention-controls">
<label id="label" class="multiple">Start typing <code>@@</code>, <code>#</code>, <code>$</code> or <code>%</code> to select the respective values</label>
<div id="multipleList" class="dropdown-sample-target" contenteditable="true" role="textbox" aria-label="Type here"></div>
@Html.UiMention(new UiMentionOptions
{
Id = "employee",
Target = "multipleList",
MentionChar = "@",
PopupWidth = "250px",
PopupHeight = "215px",
SuggestionCount = 15,
DataUrl = Url.Action("EmployeeData", "DropdownsDemo"),
Fields = new UiDropDownFields { Text = "FirstName", Value = "EmployeeID" }
})
@Html.UiMention(new UiMentionOptions
{
Id = "project",
Target = "multipleList",
MentionChar = "#",
AllowSpaces = true,
DisplayTemplate = "#displayProject",
RequireLeadingSpace = false,
DataSource = (IEnumerable<DropdownsDemoController.ProjectsData>)ViewData["project"],
Fields = new UiDropDownFields { Text = "Value" }
})
@Html.UiMention(new UiMentionOptions
{
Id = "cost",
Target = "multipleList",
MentionChar = "$",
AllowSpaces = true,
DisplayTemplate = "#displayCost",
DataSource = (IEnumerable<DropdownsDemoController.UseCostsData>)ViewData["cost"],
Fields = new UiDropDownFields { Text = "Value" }
})
@Html.UiMention(new UiMentionOptions
{
Id = "status",
Target = "multipleList",
MentionChar = "%",
AllowSpaces = true,
DisplayTemplate = "#displayStatus",
DataSource = (IEnumerable<DropdownsDemoController.StatusData>)ViewData["status"],
Fields = new UiDropDownFields { Text = "Value" }
})
</div>
</div>
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 MentionController : Controller
{
// The page is Views/Mention/MultipleList.cshtml; it draws what ViewData holds.
public ActionResult MultipleList()
{
ViewData["project"] = new ProjectsData().ProjectList();
ViewData["cost"] = new UseCostsData().CostList();
ViewData["status"] = new StatusData().StatusList();
return View();
}
/// <summary>A row of the Employees data.</summary>
public class EmployeeRow
{
public int EmployeeID { get; set; }
public string FirstName { get; set; }
public string Designation { get; set; }
public string Country { get; set; }
}
/// <summary>DataUrl of the remote samples of the Dropdowns: the employees, with the typed text as a "where" on FirstName.</summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult EmployeeData(UiDataRequest request)
{
return Json(UiDataOperations.Execute(EmployeeRows.AsQueryable(), request, "FirstName"));
}
private static readonly List<EmployeeRow> EmployeeRows = new List<EmployeeRow>
{
new EmployeeRow { EmployeeID = 1, FirstName = "Andrew Fuller", Designation = "Team Lead", Country = "England" },
new EmployeeRow { EmployeeID = 2, FirstName = "Anne Dodsworth", Designation = "Developer", Country = "USA" },
new EmployeeRow { EmployeeID = 3, FirstName = "Janet Leverling", Designation = "HR", Country = "USA" },
// ... 406 more rows
};
public class ProjectsData
{
public string Id { get; set; }
public string Value { get; set; }
public List<ProjectsData> ProjectList()
{
List<ProjectsData> project = new List<ProjectsData>();
project.Add(new ProjectsData { Id = "Project1", Value = "ERP" });
project.Add(new ProjectsData { Id = "Project2", Value = "Help desk" });
project.Add(new ProjectsData { Id = "Project3", Value = "Banking" });
project.Add(new ProjectsData { Id = "Project4", Value = "Chat Box" });
project.Add(new ProjectsData { Id = "Project5", Value = "Accounts" });
return project;
}
}
public class UseCostsData
{
public string Id { get; set; }
public string Value { get; set; }
public List<UseCostsData> CostList()
{
List<UseCostsData> usecost = new List<UseCostsData>();
usecost.Add(new UseCostsData { Id = "Cost1", Value = "$1000" });
usecost.Add(new UseCostsData { Id = "Cost2", Value = "$1500" });
usecost.Add(new UseCostsData { Id = "Cost3", Value = "$3000" });
usecost.Add(new UseCostsData { Id = "Cost4", Value = "$3250" });
usecost.Add(new UseCostsData { Id = "Cost5", Value = "$5000" });
return usecost;
}
}
public class StatusData
{
public string Id { get; set; }
public string Value { get; set; }
public List<StatusData> StatusList()
{
List<StatusData> status = new List<StatusData>();
status.Add(new StatusData { Id = "Status1", Value = "Open" });
status.Add(new StatusData { Id = "Status2", Value = "In-progress" });
status.Add(new StatusData { Id = "Status3", Value = "Hold" });
status.Add(new StatusData { Id = "Status4", Value = "Closed" });
return status;
}
}
}
}
@* 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>
@* Dropdowns with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
The RequireLeadingSpace property in Mention controls whether a space is needed before triggering the Mention suggestion popup. When set to false, it activates without a space; when set to true, a space is required before the Mention character. To see this feature in action, start typing with #.
In the above sample, the following are configured for the contenteditable div element with mention integrated.
@- Typing@lists out the suggestions of the employee names.#- Typing#lists the project names.$- Typing$lists out the cost of the project.%- Typing%lists the status of the project.