对于我的公司,我们正在为一个发票系统构建一个 Web 应用程序,该系统使用类似于 Facebook 的通知栏,用户可以在其中下拉一个菜单,该菜单显示系统上的交易状态以及未完成交易的总数旁边显示。详情请看图片。
http://img210.imageshack.us/img210/4379/alertdrop.png
我们正在使用下拉菜单中的表的以下代码检索数据:
<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>
以下是跨度,显示总数:
<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>
此代码当前存储在全局“_layout.cshtml”文件中。请原谅代码的粗糙,这是一个非常早期的版本。但是,这在检索页面加载数据方面效果很好。但是,系统要求这些信息每隔几秒自动更新一次,而无需刷新整个页面。本质上,调用控制器以带回当前数据并使用当前值更新<table>
和。<span>
我被要求创建一个 Ajax 函数,该函数从“AlertController”中检索数据并相应地更新视图。请在下面找到此控制器的内容:
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);
}
}
我被难住了,但得到了以下 Ajax 函数示例,该函数用于完全执行不同的任务,以帮助我:
$.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
});
}
});
到目前为止,我唯一设法开始工作的是一个设置间隔函数,它每 5 秒发出一次警报!
对此有何指导?