Example of Ajax Content in ASP.NET MVC Tooltip Control
This sample demonstrates the AJAX functionalities of the Tooltip which will open by hover or touch-hold action on list-item.
National Sports
- Australia
- Bhutan
- China
- Cuba
- India
- Switzerland
- United States
@Html.UiTooltip(new UiTooltipOptions { Id = "Tooltip", Target = "#countrylist [data-ui-tip-key]", Container = "countrylist", Content = "Loading...", CssClass = "e-ajax-content", Position = UiTooltipPosition.RightCenter, ContentUrl = Url.Action("TooltipSport", "LayoutDemo") })
<style>
.control-section { padding: 10px 15px; }
.property-row { display: flex; gap: 12px; align-items: center; justify-content: space-between; padding: 8px 0; }
.list-header { text-align: center; }
.country-list { list-style: none; max-width: 240px; margin: 0 auto; padding: 0; border: 1px solid var(--ui-border); border-radius: 4px; background: var(--ui-surface); }
.country-list li { padding: 10px 14px; border-bottom: 1px solid var(--ui-border); cursor: default; }
.country-list li:last-child { border-bottom: 0; }
.country-list li:hover { background: var(--ui-row-hover-bg); }
.contentWrap .def { font-weight: 600; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
<div class="control-section">
<h4 class="list-header">National Sports</h4>
<ul id="countrylist" class="country-list">
@foreach (var country in (List<LayoutDemoController.TooltipCountry>)ViewData["data"])
{
<li data-ui-tip-key="@country.Id">@country.Text</li>
}
</ul>
@Html.UiTooltip(new UiTooltipOptions { Id = "Tooltip", Target = "#countrylist [data-ui-tip-key]", Container = "countrylist", Content = "Loading...", CssClass = "e-ajax-content", Position = UiTooltipPosition.RightCenter, ContentUrl = Url.Action("TooltipSport", "LayoutDemo") })
</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 TooltipController : Controller
{
// The page is Views/Tooltip/AjaxContent.cshtml; it draws what ViewData holds.
public ActionResult AjaxContent()
{
ViewData["data"] = new List<TooltipCountry>
{
new TooltipCountry { Id = "1", Text = "Australia" },
new TooltipCountry { Id = "2", Text = "Bhutan" },
new TooltipCountry { Id = "3", Text = "China" },
new TooltipCountry { Id = "4", Text = "Cuba" },
new TooltipCountry { Id = "5", Text = "India" },
new TooltipCountry { Id = "6", Text = "Switzerland" },
new TooltipCountry { Id = "7", Text = "United States" }
};
return View();
}
/// <summary>A country of the Ajax Content sample's list.</summary>
public class TooltipCountry
{
public string Id { get; set; }
public string Text { get; set; }
}
private static readonly Dictionary<string, string> NationalSports = new Dictionary<string, string>
{
{ "1", "Cricket" }, { "2", "Archery" }, { "3", "Table tennis" }, { "4", "Baseball" }, { "5", "Hockey" }, { "6", "Shooting" }, { "7", "Baseball" }
};
/// <summary>ContentUrl of the Ajax Content tooltip sample: the national sport of the country whose data-ui-tip-key is sent as key (Syncfusion reads tooltipdata.json).</summary>
[HttpGet]
public ActionResult TooltipSport(string key)
{
string sport;
if (key == null || !NationalSports.TryGetValue(key, out sport)) { return HttpNotFound(); }
return Content("<div class=\"contentWrap\"><div class=\"def\">" + System.Net.WebUtility.HtmlEncode(sport) + "</div></div>", "text/html");
}
}
}
@* 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>
@* Layouts with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
This sample illustrates the way to load the content of a tooltip dynamically using AJAX request. Here, when the user hovers/tap on the country names, its respective data (national game of each country and its related game icon) will be retrieved dynamically and then assigned to the tooltip’s content.
The AJAX request should be made within the beforeRender event of the tooltip, and on every success, the corresponding retrieved data will be set to the content property of the tooltip.