Example of Template in ASP.NET MVC Message Control
This sample demonstrates the template functionality of the ASP.NET MVC Message control. Click the dismiss button to hide the message. Click the Show pull request button to restore the hidden message.
@Html.UiButton(new UiButtonOptions { Id = "showBtn", Text = "Show pull request", Variant = UiButtonVariant.Outline, CssClass = "msg-hidden" })
@Html.UiMessage(new UiMessageOptions
{
Id = "msg_template", Severity = UiSeverity.Success, OnClosed = "closed",
Template = @<div id="template"><span class="msg-title">Merged pull request</span><p>Pull request #41 merged after a successful build</p><button type="button" id="commitBtn" class="ui-btn ui-btn-link">View Commit</button><button type="button" id="btn1" class="ui-btn ui-btn-link">Dismiss</button></div>
})
<style>
.msg-content-section { margin: 0 auto; max-width: 520px; padding-top: 10px; }
.msg-content-section .ui-message { margin: 10px 0; }
.msg-content-section h4 { margin: 12px 0 4px; font-size: 14px; }
.msg-hidden { display: none; }
.property-section .property-row { display: flex; align-items: center; gap: 12px; padding: 6px 0; }
.msg-template-section .msg-content-section { max-width: 450px; padding-top: 20px; }
.msg-template-section .ui-message .msg-title { margin: 0; font-size: 16px; font-weight: 600; line-height: 1.25; display: block; }
.msg-template-section .ui-message p { margin: 8px 0 4px; }
.msg-template-section .ui-message .ui-btn { padding: 0 5px 0 0; }
</style>
<div class="control-section msg-template-section">
<div class="msg-content-section">
@Html.UiButton(new UiButtonOptions { Id = "showBtn", Text = "Show pull request", Variant = UiButtonVariant.Outline, CssClass = "msg-hidden" })
@Html.UiMessage(new UiMessageOptions
{
Id = "msg_template", Severity = UiSeverity.Success, OnClosed = "closed",
Template = @<div id="template"><span class="msg-title">Merged pull request</span><p>Pull request #41 merged after a successful build</p><button type="button" id="commitBtn" class="ui-btn ui-btn-link">View Commit</button><button type="button" id="btn1" class="ui-btn ui-btn-link">Dismiss</button></div>
})
</div>
</div>
<script>
// Syncfusion: Dismiss hides the message and brings back the Show button
function closed() { document.getElementById('showBtn').classList.remove('msg-hidden'); }
document.addEventListener('click', function (e) {
var hit = e.target.closest ? e.target.closest('button') : null;
if (!hit) { return; }
if (hit.id === 'btn1') { window.Lumen.get('msg_template').hide(); closed(); }
else if (hit.id === 'showBtn') { window.Lumen.get('msg_template').show(); hit.classList.add('msg-hidden'); }
});
</script>
<script>
(function (window, document) {
'use strict';
document.addEventListener('click', function (e) {
var action = e.target.closest('[data-message-action]');
if (!action) { return; }
window.NotificationSampleLog('Message action: ' + action.getAttribute('data-message-action') + ' (simulated).');
var message = window.Lumen.get('messageTemplate');
if (message) { message.hide(); }
});
})(window, document);
</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 MessageController : Controller
{
// The page is Views/Message/Template.cshtml; it draws what ViewData holds.
public ActionResult Template()
{
return View();
}
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. *@
The Message control has an option to customize the content with a custom structure. The content can be a string, paragraph, or any other HTML element.
In this sample, the Message control content is customized with HTML elements and Button controls.