-
-
Save 13orno/d44d0117f17bcd9d0cb7 to your computer and use it in GitHub Desktop.
ASP.NET MVC Flash Messages (Razor)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
$(function () { | |
$("#alert_placeholder").flashMessage(); | |
}); | |
</script> | |
<div id="alert_placeholder"></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Html.Partial("_Flash") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage within MVC Controller | |
[HttpPost] | |
public ActionResult Create() | |
{ | |
return RedirectToAction("DashBoard", "agent").Danger("Danger message.").Success("this is a success").Warning("it's a warning"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function( $ ) { | |
$.fn.showalert = function (message, alerttype) { | |
var target = this; | |
target.append('<div class="alert alert-' + alerttype.toString().toLowerCase() + ' fade in removealert"><a class="close" data-dismiss="alert">×</a><span>' + message + '</span></div>') | |
$(".removealert").delay(5000).fadeOut(400); | |
} | |
$.fn.flashMessage = function (options) { | |
var target = this; | |
options = $.extend({ timeout: 3000, alert: 'info' }, options); | |
if (!options.message) { | |
setFlashMessageFromCookie(options); | |
} | |
// 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 (!jQuery.isEmptyObject(cookie)) { | |
options.message = cookie; | |
options.alert = alert; | |
target.showalert(options.message, options.alert); | |
deleteFlashMessageCookie(alert); | |
return; | |
} | |
}); | |
} | |
// Delete the named flash cookie | |
function deleteFlashMessageCookie(alert) { | |
//$.cookie("Flash." + alert, null, { path: '/' }); | |
$.removeCookie("Flash." + alert, { path: '/' }); | |
} | |
}; | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment