For my company, we are building a web application for an invoicing system which utilises a notification bar, similar to that of Facebook, whereby the user can drop down a menu which displays the status of transactions on the system while a total number of outstanding transactions displays next to it. Please see the image for details.
http://img210.imageshack.us/img210/4379/alertdrop.png
We are retrieving the data for this using the following code for the table within the drop-down menu:
<table id="alertDropTable" style="margin-top:10px;">
@{
Dictionary<string, int> dic = ViewBag.statusCount;
if (dic != null)
{
for (int i = 0; i < dic.Count; i++)
{
if (dic.Values.ElementAt(i) > 0)
{
<tr>
<td width="100%" style="padding-bottom:4px; padding-top:4px; padding-left:10px; padding-right:10px; vertical-align:middle;">
You have <strong><a style="background-color:#878787; border-radius:5px; color:White; padding:3px;">@dic.Values.ElementAt(i)</a></strong> @dic.Keys.ElementAt(i) transaction(s).
</td>
</tr>
}
}
}
}
</table>
And the following for the span, which displays the total:
<div style="float: left;">
<a href="javascript:;" onmouseover="document.alert.src='@Url.Content("~/Content/images/alert_notification_hover.png")'" onmouseout="document.alert.src='@Url.Content("~/Content/images/alert_notification.png")'" onclick="toggle('notificationDrop');"><img src="@Url.Content("~/Content/images/alert_notification.png")" name="alert" alt="alert"/></a>
@{
Dictionary<string, int> dicheader = ViewBag.statusCount;
int diccount = 0;
if (dicheader != null)
{
for (int i = 0; i < dicheader.Count; i++)
{
if (dicheader.Values.ElementAt(i) > 0)
{
diccount = diccount + @dicheader.Values.ElementAt(i);
}
}
}
}
</div>
<div id="alertTotalDiv" style="float:left;margin-top:6px; margin-left:5px;"><span id="alertTotal" style="vertical-align:middle; background-color:#878787; border-radius:5px; color:White; font-family:Georgia; font-size:20px; padding:3px; padding-left:5px; padding-right:5px;margin-top:0px;">@diccount</span></div>
This code is currently stored in the global "_layout.cshtml" file. Please excuse the roughness of the code, this is very much an early version. However, this is working fine in terms of retrieving the data on a page load. However, the system requires this information to be automatically updated every few seconds without refreshing the entire page. In essence, making a call to the controller to bring back the current data and update the <table>
and <span>
with the current values.
I have been asked to create an Ajax function which retrieves the data from the "AlertController" and updates the view accordingly. Please find the contents of this controller below:
public class AlertController : Controller
{
/// <summary>
/// Gets or sets the user service contract.
/// </summary>
/// <value>The user service contract.</value>
public IUserServiceContract UserServiceContract { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="BaseController"/> class.
/// </summary>
protected AlertController()
{
this.UserServiceContract = Gateway.Instance.Resolve<IUserServiceContract>();
}
/// <summary>
/// Get the AlertTypes
/// </summary>
/// <returns></returns>
public virtual void ViewAlerts()
{
Gateway.Instance.Logger.LogInfo(string.Format("Overview Controller View: Fetching list of alerts."));
try
{
if (this.UserServiceContract != null)
{
var allAnnouncements = this.UserServiceContract.GetAnnoucements();
var userAlertSettings = this.UserServiceContract.GetUserAlert();
ViewBag.statusCount = userAlertSettings;
ViewBag.announcements = allAnnouncements.ToList();
}
}
catch (Exception ex)
{
Gateway.Instance.Logger.LogInfo(ex);
throw new Exception(string.Format("Home Controller View Error: {0} occured while fetching alerts.", ex.Message), ex);
}
}
I am stumped, but have been given the following example of an Ajax function, which is used to perform a different task entirely, to aid me:
$.ajax({
url: '@Url.Action("ViewAlerts", "Alerts")',
data: { ownerIds: ownerIds },
traditional: true,
dataType: 'json',
success: function (result) {
for (i = 0; i < ownerIds.length; i++) {
$(".ownersTable tr[id='tablerow+" + ownerIds[i] + "']").remove();
}
},
error: function (xhr, ajaxOptions, thrownError) {
$("#confirmDiv").alertModal({
heading: '@Language.Fail',
body: '@Language.AlertRemoveOwnerFailed' + thrownError
});
}
});
Thus far, the only thing I've managed to get working was a set interval function, which makes an alert every 5 seconds!
Any guidance on this?