Skip to content

Commit

Permalink
added flashes with a couple of examples
Browse files Browse the repository at this point in the history
  • Loading branch information
djpnewton committed Oct 19, 2017
1 parent 3132901 commit 3672bd3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public async Task<IActionResult> Login(LoginViewModel model, string returnUrl =
if (result.Succeeded)
{
_logger.LogInformation("User logged in.");
this.FlashSuccess("Logged in");
return RedirectToLocal(returnUrl);
}
if (result.RequiresTwoFactor)
Expand Down Expand Up @@ -247,6 +248,7 @@ public async Task<IActionResult> Logout()
{
await _signInManager.SignOutAsync();
_logger.LogInformation("User logged out.");
this.FlashSuccess("Logged out");
return RedirectToAction(nameof(HomeController.Index), "Home");
}

Expand Down
38 changes: 38 additions & 0 deletions Extensions/ControllerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace Microsoft.AspNetCore.Mvc
{
public static class FlashMessages
{
public static string Info = "InfoMessage";
public static string Success = "SuccessMessage";
public static string Error = "ErrorMessage";
public static string Warning = "WarningMessage";
}

//TODO: allow mulitple flashes of same type???
public static class ControllerExtensions
{
static void FlashMessage(this Controller controller, string type, string message)
{
controller.TempData[type] = message;
}
public static void FlashInfo(this Controller controller, string message)
{
FlashMessage(controller, FlashMessages.Info, message);
}

public static void FlashSuccess(this Controller controller, string message)
{
FlashMessage(controller, FlashMessages.Success, message);
}

public static void FlashError(this Controller controller, string message)
{
FlashMessage(controller, FlashMessages.Error, message);
}

public static void FlashWarning(this Controller controller, string message)
{
FlashMessage(controller, FlashMessages.Warning, message);
}
}
}
21 changes: 21 additions & 0 deletions Views/Shared/_FlashMessages.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@using Microsoft.AspNetCore.Mvc.RazorPages

@if (TempData.ContainsKey(FlashMessages.Error))
{
<div class="alert alert-danger">@TempData[FlashMessages.Error]</div>
}

@if (TempData.ContainsKey(FlashMessages.Info))
{
<div class="alert alert-info">@TempData[FlashMessages.Info]</div>
}

@if (TempData.ContainsKey(FlashMessages.Warning))
{
<div class="alert alert-warning">@TempData[FlashMessages.Warning]</div>
}

@if (TempData.ContainsKey(FlashMessages.Success))
{
<div class="alert alert-success" role="alert">@TempData[FlashMessages.Success]</div>
}
3 changes: 3 additions & 0 deletions Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
</div>
</div>
</nav>

@await Html.PartialAsync("_FlashMessages")

<div class="container body-content">
@RenderBody()
<hr />
Expand Down

0 comments on commit 3672bd3

Please sign in to comment.