Skip to content

Commit b845d40

Browse files
committed
Add Notifications
Using toastr library
1 parent 73349ff commit b845d40

16 files changed

+16834
-14
lines changed

README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,45 @@
11
WebHelpers
22
==========
33

4-
Paging - like stackoverflow
4+
+ Paging - like stackoverflow
5+
+ Notifications
6+
7+
8+
Notifications
9+
-------------
10+
11+
Uses the Javascript library toastr from [https://github.com/CodeSeven/toastr](https://github.com/CodeSeven/toastr)
12+
13+
14+
Controller:
15+
16+
17+
using dks.Web.Extensions;
18+
19+
public class HomeController : Controller
20+
{
21+
//
22+
// GET: /Home/
23+
public ActionResult Index()
24+
{
25+
26+
this.AddNotification("Welcome Guest");
27+
return View();
28+
}
29+
}
30+
31+
32+
View:
33+
34+
35+
<link href="~/content/toastr.css" rel="stylesheet" />
36+
<script src="~/Scripts/jquery-1.6.4.js"></script>
37+
<script src="~/Scripts/toastr.js"></script>
38+
39+
40+
@Html.RenderToasts()
41+
42+
![](notifications.gif)
543

644

745

WebHelpers/Notifications.Toastr.cs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Web;
6+
using System.Web.Mvc;
7+
8+
namespace dks.Web.Extensions
9+
{
10+
11+
// Requires toastr.js to be included.
12+
13+
14+
public static class Notifications
15+
{
16+
17+
public enum Notification
18+
{
19+
Success,
20+
Info,
21+
Warning,
22+
Error
23+
}
24+
25+
[Serializable]
26+
private struct NotificationMessage
27+
{
28+
public Notification notification;
29+
public String message;
30+
}
31+
32+
private static readonly string notificationKey = "_Notifications_toastr";
33+
34+
35+
public static HtmlString RenderToasts(this HtmlHelper helper)
36+
{
37+
var notifications = helper.ViewContext.TempData[notificationKey] as List<NotificationMessage>;
38+
if (notifications != null && notifications.Count > 0)
39+
{
40+
StringBuilder sb;
41+
42+
sb = new StringBuilder();
43+
sb.AppendLine("<script type=\"text/javascript\">");
44+
45+
foreach (var notification in notifications)
46+
{
47+
switch (notification.notification)
48+
{
49+
case Notification.Info:
50+
sb.AppendFormat("\ttoastr.info('{0}');\n", notification.message);
51+
break;
52+
case Notification.Warning:
53+
sb.AppendFormat("\ttoastr.warning('{0}');\n", notification.message);
54+
break;
55+
case Notification.Success:
56+
sb.AppendFormat("\ttoastr.success('{0}');\n", notification.message);
57+
break;
58+
case Notification.Error:
59+
sb.AppendFormat("\ttoastr.error('{0}');\n", notification.message);
60+
break;
61+
62+
}
63+
}
64+
sb.AppendLine("</script>");
65+
66+
return new HtmlString(sb.ToString());
67+
}
68+
69+
70+
return new HtmlString("");
71+
}
72+
73+
public static void AddNotification(this Controller controller, String message)
74+
{
75+
AddNotification(controller, Notification.Success, message);
76+
}
77+
78+
79+
public static void AddNotification(this Controller controller, Notification notification, String message)
80+
{
81+
if (!string.IsNullOrWhiteSpace(message))
82+
{
83+
var notifications = controller.TempData[notificationKey] as List<NotificationMessage>;
84+
if (notifications == null)
85+
{
86+
notifications = new List<NotificationMessage>();
87+
controller.TempData[notificationKey] = notifications;
88+
}
89+
90+
notifications.Add(new NotificationMessage() { notification = notification, message = message });
91+
}
92+
}
93+
94+
}
95+
}

WebHelpers/dksWebHelpers.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<Compile Include="PageSizer.cs" />
4949
<Compile Include="PagingExtensions.cs" />
5050
<Compile Include="Properties\AssemblyInfo.cs" />
51+
<Compile Include="Notifications.Toastr.cs" />
5152
</ItemGroup>
5253
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5354
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

WebHelpersDemo/Controllers/HomeController.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,26 @@
33
using System.Linq;
44
using System.Web;
55
using System.Web.Mvc;
6+
using dks.Web.Extensions;
7+
68

79
namespace WebHelpers.Controllers
810
{
9-
public class HomeController : Controller
10-
{
11-
//
12-
// GET: /Home/
11+
public class HomeController : Controller
12+
{
13+
//
14+
// GET: /Home/
15+
16+
public ActionResult Index()
17+
{
18+
19+
this.AddNotification("Welcome Guest"); // default is Success
20+
this.AddNotification(Notifications.Notification.Error, "Error Guest");
21+
this.AddNotification(Notifications.Notification.Info, "Info Guest");
22+
this.AddNotification(Notifications.Notification.Warning, "Warning Guest");
23+
return View();
24+
}
1325

14-
public ActionResult Index()
15-
{
16-
return View();
17-
}
1826

19-
20-
}
27+
}
2128
}

0 commit comments

Comments
 (0)