Created
December 16, 2010 04:44
-
-
Save detroitpro/743041 to your computer and use it in GitHub Desktop.
Rob Conery's ASP.NET MVC FlashHelpers class - ala: rails
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Text; | |
namespace System.Web.Mvc { | |
public static class FlashHelpers { | |
public static void FlashInfo(this Controller controller,string message) { | |
controller.TempData["info"] = message; | |
} | |
public static void FlashWarning(this Controller controller, string message) { | |
controller.TempData["warning"] = message; | |
} | |
public static void FlashError(this Controller controller, string message) { | |
controller.TempData["error"] = message; | |
} | |
public static string Flash(this HtmlHelper helper) { | |
var message = ""; | |
var className = ""; | |
if (helper.ViewContext.TempData["info"] != null) { | |
message =helper.ViewContext.TempData["info"].ToString(); | |
className = "info"; | |
} else if (helper.ViewContext.TempData["warning"] != null) { | |
message = helper.ViewContext.TempData["warning"].ToString(); | |
className = "warning"; | |
} else if (helper.ViewContext.TempData["error"] != null) { | |
message = helper.ViewContext.TempData["error"].ToString(); | |
className = "error"; | |
} | |
var sb = new StringBuilder(); | |
if (!String.IsNullOrEmpty(message)) { | |
sb.AppendLine("<script>"); | |
sb.AppendLine("$(document).ready(function() {"); | |
sb.AppendFormat("$('#flash').html('{0}');", message); | |
sb.AppendFormat("$('#flash').toggleClass('{0}');", className); | |
sb.AppendLine("$('#flash').slideDown('slow');"); | |
sb.AppendLine("$('#flash').click(function(){$('#flash').toggle('highlight')});"); | |
sb.AppendLine("});"); | |
sb.AppendLine("</script>"); | |
} | |
return sb.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment