-
-
Save slashdotdash/d333bcf22ca167aa7f01 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
<div id="flash-messages" class="alert-message" style="display:none;"> | |
<a class="close" href="#">×</a> | |
<p></p> | |
</div> | |
<script type="text/javascript"> | |
$(function () { | |
$("#flash-messages").flashMessage(); | |
}); | |
</script> |
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("Index").Success("Message shown to user after redirect"); | |
} |
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 Error(this ActionResult result, string message) | |
{ | |
CreateCookieWithFlashMessage(Notification.Error, 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 | |
{ | |
Error, | |
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
$.fn.flashMessage = function (options) { | |
var target = this; | |
options = $.extend({}, options, { timeout: 3000, alert: 'info' }); | |
if (!options.message) { | |
setFlashMessageFromCookie(options); | |
} | |
if (options.message) { | |
$(target).addClass(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', 'Error', 'Warning', 'Info'), function (i, alert) { | |
var cookie = $.cookie("Flash." + alert); | |
if (cookie) { | |
options.message = cookie; | |
options.alert = alert; | |
deleteFlashMessageCookie(alert); | |
return; | |
} | |
}); | |
} | |
// Delete the named flash cookie | |
function deleteFlashMessageCookie(alert) { | |
$.cookie("Flash." + alert, null, { path: '/' }); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm using one of the fixes done to the original code (by thomkaufmann) and wanted to know the license terms if the code snippet can be used in other projects.
Thanks