Skip to content

Instantly share code, notes, and snippets.

@thomkaufmann
Forked from slashdotdash/ExampleController.cs
Last active November 5, 2019 09:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thomkaufmann/a1d301789fc3066ebdd5 to your computer and use it in GitHub Desktop.
Save thomkaufmann/a1d301789fc3066ebdd5 to your computer and use it in GitHub Desktop.
<div id="flash-messages" class="alert" style="display:none;">
<a class="close" href="#">&times;</a>
<p></p>
</div>
<script type="text/javascript">
$(function () {
$("#flash-messages").flashMessage();
});
</script>
@Html.Partial("_Flash")
// Usage within MVC Controller
[HttpPost]
public ActionResult Create()
{
return RedirectToAction("Index").Success("Message shown to user after redirect");
}
internal static class FlashMessageExtensions
{
public static ActionResult Danger(this ActionResult result, string message)
{
CreateCookieWithFlashMessage(Notification.Danger, message);
return result;
}
public static ActionResult Warning(this ActionResult result, string message)
{
CreateCookieWithFlashMessage(Notification.Warning, message);
return result;
}
public static ActionResult Success(this ActionResult result, string message)
{
CreateCookieWithFlashMessage(Notification.Success, message);
return result;
}
public static ActionResult Information(this ActionResult result, string message)
{
CreateCookieWithFlashMessage(Notification.Info, message);
return result;
}
private static void CreateCookieWithFlashMessage(Notification notification, string message)
{
HttpContext.Current.Response.Cookies.Add(new HttpCookie(string.Format("Flash.{0}", notification), message) { Path = "/" });
}
private enum Notification
{
Danger,
Warning,
Success,
Info
}
}
$.fn.flashMessage = function (options) {
var target = this;
options = $.extend({ timeout: 3000, alert: 'info' }, options);
if (!options.message) {
setFlashMessageFromCookie(options);
}
if (options.message) {
$(target).addClass("alert-" + options.alert.toString().toLowerCase());
if (typeof options.message === "string") {
$('p', target).html("<span>" + options.message + "</span>");
} else {
target.empty().append(options.message);
}
} else {
return;
}
if (target.children().length === 0) return;
target.fadeIn().one("click", function () {
$(this).fadeOut();
});
if (options.timeout > 0) {
setTimeout(function () { target.fadeOut(); }, options.timeout);
}
return this;
// Get the first alert message read from the cookie
function setFlashMessageFromCookie() {
$.each(new Array('Success', 'Danger', 'Warning', 'Info'), function (i, alert) {
var cookie = $.cookie("Flash." + alert);
if (cookie != "null") {
options.message = cookie;
options.alert = alert;
deleteFlashMessageCookie(alert);
return;
}
});
}
// Delete the named flash cookie
function deleteFlashMessageCookie(alert) {
$.cookie("Flash." + alert, null, { path: '/' });
}
};
@thomkaufmann
Copy link
Author

Updated for jQuery Cookie Plugin v1.4.0 and Bootstrap 3.0.3

@13orno
Copy link

13orno commented Aug 13, 2014

Made some change to show multiple message like below.

return RedirectToAction("DashBoard", "agent").Danger("Danger message.").Success("this is a success").Warning("it's a warning");

https://gist.github.com/13orno/d44d0117f17bcd9d0cb7

@siddhant4u
Copy link

Hello, Thanks for the code. Could you specify the license terms if someone want to use the code?

Thanks

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