Skip to content

Instantly share code, notes, and snippets.

@saad749
Created August 22, 2016 12:43
Show Gist options
  • Save saad749/89293084defd99378df73b06d9e35fac to your computer and use it in GitHub Desktop.
Save saad749/89293084defd99378df73b06d9e35fac to your computer and use it in GitHub Desktop.
Easy Generic way to Display Alerts in ASP.Net Core
@if (ViewBag.Messages != null)
{
@foreach (var msg in ViewBag.Messages)
{
<div class="alert alert-@msg.AlertType alert-dismissible fade in" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<p><strong>@msg.AlertTitle</strong> @msg.AlertMessage</p>
</div>
}
}
@{
Html.RenderPartial("_Alerts");
}
public class AlertViewModel
{
public string AlertType { get; set; }
public string AlertTitle { get; set; }
public string AlertMessage { get; set; }
public AlertViewModel(string type, string title, string message)
{
AlertType = type;
AlertTitle = title;
AlertMessage = message;
}
}
public IActionResult Index()
{
ViewBag.Messages = new[] {
new AlertViewModel("success", "Success!", "The object was added successfully!"),
new AlertViewModel("warning", "Warning!", "The object was added with a warning!"),
new AlertViewModel("danger", "Danger!", "The object was not added!")
};
return View();
}
@BennyMargalit
Copy link

look very nice and clear code but how you show it ?

@bungard
Copy link

bungard commented Aug 28, 2020

look very nice and clear code but how you show it ?

conditional display in _alerts.cshtml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment