Skip to content

Instantly share code, notes, and snippets.

@avisser
Created February 3, 2016 17:03
Show Gist options
  • Save avisser/964b9fc659cb76193df3 to your computer and use it in GitHub Desktop.
Save avisser/964b9fc659cb76193df3 to your computer and use it in GitHub Desktop.
var toastTypes = new[] { "success", "error", "info", "warning" };
foreach (var type in toastTypes)
{
if (TempData.ContainsKey(type))
{
string message = (string)TempData[type];
string title = null;
if (TempData.ContainsKey("title"))
{
title = (string)TempData["title"];
<script language="javascript">
toastr['@type']('@message', '@title');
</script>
}
else
{
<script language="javascript">
toastr['@type']('@message');
</script>
}
}
}
public static class ToastrExtension
{
public static void Success(this Controller controller, string message, string title = null)
{
controller.TempData["success"] = message;
if (title != null)
{
controller.TempData["title"] = title;
}
}
public static void Info(this Controller controller, string message, string title = null)
{
controller.TempData["info"] = message;
if (title != null)
{
controller.TempData["title"] = title;
}
}
public static void Warning(this Controller controller, string message, string title = null)
{
controller.TempData["warning"] = message;
if (title != null)
{
controller.TempData["title"] = title;
}
}
public static void Error(this Controller controller, string message, string title = null)
{
controller.TempData["error"] = message;
if (title != null)
{
controller.TempData["title"] = title;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment