Example of API in ASP.NET MVC Toast Control
This sample demonstrates all the API functionalities available in Toast.
Progress Direction
Show Animation
Hide Animation
@Html.UiToast(new UiToastOptions { Id = "toastAPI", Position = UiToastPosition.BottomRight, Timeout = 5000, NewestOnTop = true, OnClose = "onClose" })
@Html.UiTextBox("toast_input_title", null, new UiTextBoxOptions { Label = "Enter the title", FloatLabelType = UiFloatLabelType.Auto })
@Html.UiTextArea("toast_input_content", null, new UiTextAreaOptions { Label = "Enter the content", FloatLabelType = UiFloatLabelType.Auto, Rows = 3 })
@Html.UiCheckBox("closeButton", false, new UiCheckBoxOptions { Id = "closeButton", Label = "Show Close Button", OnChange = "closeOnChange" })
@Html.UiCheckBox("progressBar", false, new UiCheckBoxOptions { Id = "progressBar", Label = "Show Progress Bar", OnChange = "OnProgressChange" })
@Html.UiCheckBox("newestOnTop", true, new UiCheckBoxOptions { Id = "newestOnTop", Label = "Newest On Top", OnChange = "closeNewestOnChange" })
@Html.UiCheckBox("prevDuplicates", false, new UiCheckBoxOptions { Id = "prevDuplicates", Label = "Prevent Duplicates", OnChange = "OnPrevDubChange" })
@Html.UiCheckBox("actionButtons", false, new UiCheckBoxOptions { Id = "actionButtons", Label = "Action Buttons", OnChange = "OnactionBtnChange" })
@Html.UiDropDownList("Direction", new UiDropDownListOptions { Width = "150px", Value = "Rtl", OnChange = "onDirectionChange", Items = ((IEnumerable<NotificationsDemoController.ToastChoice>)ViewData["directionData"]).Select(c => new UiListItem(c.Id, c.Value)).ToList() })
@Html.UiNumericTextBox("timeOut", new UiNumericTextBoxOptions { Label = "TimeOut", FloatLabelType = UiFloatLabelType.Auto, Value = 5000, Min = 0, Step = 100, Format = "N0" })
@Html.UiNumericTextBox("showDuration", new UiNumericTextBoxOptions { Label = "Duration", FloatLabelType = UiFloatLabelType.Auto, Value = 400, Min = 0, Step = 50, Format = "N0" })
@Html.UiDropDownList("ShowEasing", new UiDropDownListOptions { Width = "100%", Value = "ease", OnChange = "onShowEase", Items = ((IEnumerable<NotificationsDemoController.ToastChoice>)ViewData["EasingData"]).Select(c => new UiListItem(c.Id, c.Value)).ToList() })
@Html.UiDropDownList("ShowAnimation", new UiDropDownListOptions { Width = "100%", Value = "SlideBottomIn", OnChange = "showChange", Items = ((IEnumerable<NotificationsDemoController.ToastChoice>)ViewData["AnimationData"]).Select(c => new UiListItem(c.Id, c.Value)).ToList() })
@Html.UiNumericTextBox("hideDuration", new UiNumericTextBoxOptions { Label = "Duration", FloatLabelType = UiFloatLabelType.Auto, Value = 400, Min = 0, Step = 50, Format = "N0" })
@Html.UiDropDownList("HideEasing", new UiDropDownListOptions { Width = "100%", Value = "ease", OnChange = "onHideEase", Items = ((IEnumerable<NotificationsDemoController.ToastChoice>)ViewData["EasingData"]).Select(c => new UiListItem(c.Id, c.Value)).ToList() })
@Html.UiDropDownList("HideAnimation", new UiDropDownListOptions { Width = "100%", Value = "FadeIn", OnChange = "hideChange", Items = ((IEnumerable<NotificationsDemoController.ToastChoice>)ViewData["AnimationData"]).Select(c => new UiListItem(c.Id, c.Value)).ToList() })
@Html.UiButton(new UiButtonOptions { Id = "toastBtnShow", Text = "Show Toasts", Color = UiButtonColor.Primary })
@Html.UiButton(new UiButtonOptions { Id = "toastBtnHide", Text = "Hide All", Color = UiButtonColor.Primary })
<style>
.toast-default-section .control_wrapper, .toast-pos-section .control_wrapper, .toast-api-section .control_wrapper, .toast-template-section .control_wrapper { max-width: 760px; margin: 0 auto; padding: 20px 0; }
.toast-center { text-align: center; }
.toast-center .ui-btn, .toast-center button { margin: 5px; }
.toast-panel-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 6px 0; }
.toast-icon-meeting { background-color: currentColor; -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M17 12h-5v5h5v-5zM16 1v2H8V1H6v2H5c-1.11 0-1.99.9-1.99 2L3 19c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2h-1V1h-2zm3 18H5V8h14v11z'/%3E%3C/svg%3E") no-repeat center / contain; mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M17 12h-5v5h5v-5zM16 1v2H8V1H6v2H5c-1.11 0-1.99.9-1.99 2L3 19c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2h-1V1h-2zm3 18H5V8h14v11z'/%3E%3C/svg%3E") no-repeat center / contain; }
.toast-api-columns { display: flex; gap: 32px; flex-wrap: wrap; }
.toast-api-column { flex: 1 1 280px; min-width: 0; }
.toast-api-column h4 { margin: 12px 0 4px; font-size: 14px; }
#toast-action-result { margin-top: 8px; font-size: 13px; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
<div class="control-section toast-api-section">
<div class="control_wrapper">
@Html.UiToast(new UiToastOptions { Id = "toastAPI", Position = UiToastPosition.BottomRight, Timeout = 5000, NewestOnTop = true, OnClose = "onClose" })
<div class="toast-api-columns">
<div class="toast-api-column">
<div class="toast-panel-row">@Html.UiTextBox("toast_input_title", null, new UiTextBoxOptions { Label = "Enter the title", FloatLabelType = UiFloatLabelType.Auto })</div>
<div class="toast-panel-row">@Html.UiTextArea("toast_input_content", null, new UiTextAreaOptions { Label = "Enter the content", FloatLabelType = UiFloatLabelType.Auto, Rows = 3 })</div>
<div class="toast-panel-row">@Html.UiCheckBox("closeButton", false, new UiCheckBoxOptions { Id = "closeButton", Label = "Show Close Button", OnChange = "closeOnChange" })</div>
<div class="toast-panel-row">@Html.UiCheckBox("progressBar", false, new UiCheckBoxOptions { Id = "progressBar", Label = "Show Progress Bar", OnChange = "OnProgressChange" })</div>
<div class="toast-panel-row">@Html.UiCheckBox("newestOnTop", true, new UiCheckBoxOptions { Id = "newestOnTop", Label = "Newest On Top", OnChange = "closeNewestOnChange" })</div>
<div class="toast-panel-row">@Html.UiCheckBox("prevDuplicates", false, new UiCheckBoxOptions { Id = "prevDuplicates", Label = "Prevent Duplicates", OnChange = "OnPrevDubChange" })</div>
<div class="toast-panel-row">@Html.UiCheckBox("actionButtons", false, new UiCheckBoxOptions { Id = "actionButtons", Label = "Action Buttons", OnChange = "OnactionBtnChange" })</div>
<div class="toast-panel-row"><span>Progress Direction</span> @Html.UiDropDownList("Direction", new UiDropDownListOptions { Width = "150px", Value = "Rtl", OnChange = "onDirectionChange", Items = ((IEnumerable<NotificationsDemoController.ToastChoice>)ViewData["directionData"]).Select(c => new UiListItem(c.Id, c.Value)).ToList() })</div>
<div class="toast-panel-row">@Html.UiNumericTextBox("timeOut", new UiNumericTextBoxOptions { Label = "TimeOut", FloatLabelType = UiFloatLabelType.Auto, Value = 5000, Min = 0, Step = 100, Format = "N0" })</div>
</div>
<div class="toast-api-column">
<h4>Show Animation</h4>
<div class="toast-panel-row">@Html.UiNumericTextBox("showDuration", new UiNumericTextBoxOptions { Label = "Duration", FloatLabelType = UiFloatLabelType.Auto, Value = 400, Min = 0, Step = 50, Format = "N0" })</div>
<div class="toast-panel-row">@Html.UiDropDownList("ShowEasing", new UiDropDownListOptions { Width = "100%", Value = "ease", OnChange = "onShowEase", Items = ((IEnumerable<NotificationsDemoController.ToastChoice>)ViewData["EasingData"]).Select(c => new UiListItem(c.Id, c.Value)).ToList() })</div>
<div class="toast-panel-row">@Html.UiDropDownList("ShowAnimation", new UiDropDownListOptions { Width = "100%", Value = "SlideBottomIn", OnChange = "showChange", Items = ((IEnumerable<NotificationsDemoController.ToastChoice>)ViewData["AnimationData"]).Select(c => new UiListItem(c.Id, c.Value)).ToList() })</div>
<h4>Hide Animation</h4>
<div class="toast-panel-row">@Html.UiNumericTextBox("hideDuration", new UiNumericTextBoxOptions { Label = "Duration", FloatLabelType = UiFloatLabelType.Auto, Value = 400, Min = 0, Step = 50, Format = "N0" })</div>
<div class="toast-panel-row">@Html.UiDropDownList("HideEasing", new UiDropDownListOptions { Width = "100%", Value = "ease", OnChange = "onHideEase", Items = ((IEnumerable<NotificationsDemoController.ToastChoice>)ViewData["EasingData"]).Select(c => new UiListItem(c.Id, c.Value)).ToList() })</div>
<div class="toast-panel-row">@Html.UiDropDownList("HideAnimation", new UiDropDownListOptions { Width = "100%", Value = "FadeIn", OnChange = "hideChange", Items = ((IEnumerable<NotificationsDemoController.ToastChoice>)ViewData["AnimationData"]).Select(c => new UiListItem(c.Id, c.Value)).ToList() })</div>
</div>
</div>
<div class="toast-center">
@Html.UiButton(new UiButtonOptions { Id = "toastBtnShow", Text = "Show Toasts", Color = UiButtonColor.Primary })
@Html.UiButton(new UiButtonOptions { Id = "toastBtnHide", Text = "Hide All", Color = UiButtonColor.Primary })
<div id="toast-action-result" aria-live="polite"></div>
</div>
</div>
</div>
<script>
// Syncfusion: the panel gives the toast its title and content, its buttons and bars, its time and its animations
var btnEleShow = document.getElementById('toastBtnShow');
var btnEleHide = document.getElementById('toastBtnHide');
var animation = { show: { effect: 'SlideBottomIn', duration: 400, easing: 'ease' }, hide: { effect: 'FadeIn', duration: 400, easing: 'ease' } };
var withButtons = false;
function toastObj() { return window.Lumen.get('toastAPI'); }
function number(id, fallback) { var n = parseInt(String(document.getElementById(id).value).replace(/[^0-9]/g, ''), 10); return isNaN(n) ? fallback : n; }
function showToast() {
var title = document.getElementById('toast_input_title').value;
var content = document.getElementById('toast_input_content').value;
if (title === '' && content === '') { content = 'You have created a Toast message'; }
animation.show.duration = number('showDuration', 400);
animation.hide.duration = number('hideDuration', 400);
toastObj().setOptions({ animation: animation });
var model = { title: title, content: content, timeout: number('timeOut', 5000) };
if (withButtons) { model.buttons = [{ text: 'Click Here', primary: true, click: function () { document.getElementById('toast-action-result').textContent = 'Action button is clicked'; } }]; }
toastObj().show(model);
}
function onClose() { }
btnEleShow.addEventListener('click', showToast);
btnEleHide.addEventListener('click', function () { toastObj().hideAll(); });
document.addEventListener('click', function (e) { if (!e.target.closest('#toastBtnShow') && !e.target.closest('.toast-api-section') && toastObj()) { toastObj().hideAll(); } });
function onShowEase(value) { if (value) { animation.show.easing = value; } }
function onHideEase(value) { if (value) { animation.hide.easing = value; } }
function showChange(value) { if (value) { animation.show.effect = value; } }
function hideChange(value) { if (value) { animation.hide.effect = value; } }
function onDirectionChange(value) { if (value) { toastObj().setOptions({ progressDirection: value }); } }
function closeOnChange(checked) { toastObj().setOptions({ showCloseButton: checked }); }
function OnProgressChange(checked) { toastObj().setOptions({ showProgressBar: checked }); }
function closeNewestOnChange(checked) { toastObj().setOptions({ newestOnTop: checked }); }
function OnPrevDubChange(checked) { toastObj().setOptions({ preventDuplicates: checked }); }
function OnactionBtnChange(checked) { withButtons = checked; }
</script>
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 ToastController : Controller
{
// The page is Views/Toast/API.cshtml; it draws what ViewData holds.
public ActionResult API()
{
List<ToastChoice> easingData = new List<ToastChoice>();
easingData.Add(new ToastChoice("ease", "Ease"));
easingData.Add(new ToastChoice("linear", "Linear"));
ViewData["EasingData"] = easingData;
List<ToastChoice> directionData = new List<ToastChoice>();
directionData.Add(new ToastChoice("Rtl", "Right to Left"));
directionData.Add(new ToastChoice("Ltr", "Left to Right"));
ViewData["directionData"] = directionData;
List<ToastChoice> animationData = new List<ToastChoice>();
animationData.Add(new ToastChoice("SlideBottomIn", "Slide Bottom In"));
animationData.Add(new ToastChoice("FadeIn", "Fade In"));
animationData.Add(new ToastChoice("FadeZoomIn", "Fade Zoom In"));
animationData.Add(new ToastChoice("FadeZoomOut", "Fade Zoom Out"));
animationData.Add(new ToastChoice("FlipLeftDownIn", "Flip Left Down In"));
animationData.Add(new ToastChoice("FlipLeftDownOut", "Flip Left Down Out"));
animationData.Add(new ToastChoice("FlipLeftUpIn", "Flip Left Up In"));
animationData.Add(new ToastChoice("FlipLeftUpOut", "Flip Left Up Out"));
animationData.Add(new ToastChoice("FlipRightDownIn", "Flip Right Down In"));
animationData.Add(new ToastChoice("FlipRightDownOut", "Flip Right Down Out"));
animationData.Add(new ToastChoice("FlipRightUpIn", "Flip Right Up In"));
animationData.Add(new ToastChoice("FlipRightUpOut", "Flip Right Up Out"));
animationData.Add(new ToastChoice("SlideBottomOut", "Slide Bottom Out"));
animationData.Add(new ToastChoice("SlideLeftIn", "Slide Left In"));
animationData.Add(new ToastChoice("SlideLeftOut", "Slide Left Out"));
animationData.Add(new ToastChoice("SlideRightIn", "Slide Right In"));
animationData.Add(new ToastChoice("SlideRightOut", "Slide Right Out"));
animationData.Add(new ToastChoice("SlideTopIn", "Slide Top In"));
animationData.Add(new ToastChoice("SlideTopOut", "Slide Top Out"));
animationData.Add(new ToastChoice("ZoomIn", "Zoom In"));
animationData.Add(new ToastChoice("ZoomOut", "Zoom Out"));
ViewData["AnimationData"] = animationData;
return View();
}
public class ToastChoice
{
public string Id { get; set; }
public string Value { get; set; }
public ToastChoice(string id, string value) { Id = id; Value = value; }
}
private ActionResult Show(string method)
{
NotificationSample sample = (NotificationSample)ViewBag.NotificationSample;
// A sample keeps its data in a Prepare<Page> method (NotificationsDemoController.Samples.cs); it puts what the page reads in ViewData.
string page = sample.Source.Substring(sample.Source.LastIndexOf('_') + 1);
System.Reflection.MethodInfo prepare = GetType().GetMethod(method + page, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
if (prepare != null) { prepare.Invoke(this, null); }
return View("PreparedSample", ViewData.Model);
}
}
}
@* 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>
@* Notificationss with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
In this sample, with help of text inputs toast header title and content text can be provided.
-
Action Buttons– Provide support to add a button inside toast to interact with it. -
Prevent Duplicates– Disable the user to create same toast message multiple times. -
TimeOut– Allows to set time in millisecond to close toast. -
Progress Bar– Visualizes the time out of toast as an indicator. -
Animation– Enables to define the toast show and hide animation. -
Close button– Show close button to hide toast irrespective of time out.