Skip to content

Instantly share code, notes, and snippets.

@derekgates
Last active February 19, 2016 22:37
Show Gist options
  • Save derekgates/acc7c805425997f3f3ce to your computer and use it in GitHub Desktop.
Save derekgates/acc7c805425997f3f3ce to your computer and use it in GitHub Desktop.
System for setting error/warning/info messages from MVC to view via Controller (using TempData and ViewBag). Filter is used to copy TempData entries to ViewBag on every view execution.
/// <summary>
/// Sets an error to display on the next postback. This will NOT survive a redirect, use RedirectWithErrorResult!
/// </summary>
/// <param name="error"></param>
public void SetPostBackError(string error)
{
this.ViewBag.Error = error;
}
/// <summary>
/// Sets a message to display on the next postback. This will NOT survive a redirect!
/// </summary>
/// <param name="message"></param>
public void SetPostBackInfo(string message)
{
this.ViewBag.Info = message;
}
/// <summary>
/// Sets a warning to display on the next postback. This will NOT survive a redirect!
/// </summary>
/// <param name="warning"></param>
public void SetPostBackWarning(string warning)
{
this.ViewBag.Warning = warning;
}
/// <summary>
/// Sets a success message to display on the next postback. This will NOT survive a redirect!
/// </summary>
/// <param name="warning"></param>
public void SetPostBackSuccess(string success)
{
this.ViewBag.Success = success;
}
/// <summary>
/// Sets a alert (Action Required) message to display on the next postback. This will NOT survive a redirect!
/// </summary>
/// <param name="warning"></param>
public void SetPostBackActionRequired(string action)
{
this.ViewBag.ActionRequired = action;
}
/// <summary>
/// Sets a error to display on the next redirect/postback. This will survive a redirect!
/// </summary>
/// <param name="error"></param>
public void SetRedirectError(string error)
{
this.TempData["error"] = error;
}
/// <summary>
/// Sets a warning to display on the next redirect/postback. This will survive a redirect!
/// </summary>
/// <param name="warning"></param>
public void SetRedirectWarning(string warning)
{
this.TempData["warning"] = warning;
}
/// <summary>
/// Sets a alert (Action Required) to display on the next redirect/postback. This will survive a redirect!
/// </summary>
/// <param name="warning"></param>
public void SetRedirectActionRequired(string action)
{
this.TempData["ActionRequired"] = action;
}
/// <summary>
/// Sets a message to display on the next redirect/postback. This will survive a redirect!
/// </summary>
/// <param name="message"></param>
public void SetRedirectInfo(string message)
{
this.TempData["info"] = message;
}
/// <summary>
/// Sets a succes message to display on the next redirect/postback. This will survive a redirect!
/// </summary>
/// <param name="message"></param>
public void SetRedirectSuccess(string message)
{
this.TempData["success"] = message;
}
/// <summary>
/// Copies any messages/warning/errors from TempData to the ViewBag for use with built in notifications.
/// </summary>
public class CopyMessageTempDataFilter : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var controller = filterContext.Controller;
if (controller.TempData["Error"] != null)
controller.ViewBag.Error = controller.TempData["Error"];
if (controller.TempData["Info"] != null)
controller.ViewBag.Info = controller.TempData["Info"];
if (controller.TempData["Success"] != null)
controller.ViewBag.Success = controller.TempData["Success"];
if (controller.TempData["Warning"] != null)
controller.ViewBag.Warning = controller.TempData["Warning"];
if (controller.TempData["ActionRequired"] != null)
controller.ViewBag.ActionRequired = controller.TempData["ActionRequired"];
base.OnActionExecuted(filterContext);
}
}
@if (ViewBag.Error != null)
{
<div>@HTML.NotificationBox(ViewBag.Error, "An error has occurred!", "alert-danger", false, "glyphicon-alert")</div>
}
@if (ViewBag.Warning != null)
{
<div>@HTML.NotificationBox(ViewBag.Warning, "Warning!", "alert-warning", false, "glyphicon-exclamation-sign")</div>
}
@if (ViewBag.Info != null)
{
<div>@HTML.NotificationBox(ViewBag.Info, "Info", "alert-info", false, "glyphicon-info-sign")</div>
}
@if (ViewBag.Success != null)
{
<div>@HTML.NotificationBox(ViewBag.Success, "Success!", "alert-success", false, "glyphicon-ok-circle")</div>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment