Jquery 在主页中显示通知
我正在使用信号r显示通知。我的代码摘要是Jquery 在主页中显示通知,jquery,asp.net,asp.net-mvc,sql-server-2008,signalr,Jquery,Asp.net,Asp.net Mvc,Sql Server 2008,Signalr,我正在使用信号r显示通知。我的代码摘要是 我有一个从数据库获取数据的hub方法 在我的控制器post方法中,我将数据保存到数据库中 在我的通知页面视图中,我在发送通知的按钮单击事件上有一个jquery代码 在我的主页布局上,我有一个接收通知的div选项卡(调用我的hub方法来显示值) 在这里,我面临的问题是调用了jqueryclick事件,并调用了send通知,但没有调用将数据保存到表中的httppost事件 我想显示新的通知计数(比如红色),就像我们在facebook上看到的一样,点击后,我想
public class NotificationHub:Hub
{
private MyContext db = new MyContext();
public void SendNotifications()
{
var ret = (from notification in db.Notifications.ToList()
select new
{
Message = notification.Text,
//PostedBy = post.PostedBy,
//PostedDate = post.PostedDate,
// NotificationId = notification.NotificationID,
}).ToArray();
//ret
Clients.All.receiveNotification(ret);
}
控制员职务方法:
[HttpPost]
public ActionResult Notification(Notification notification)
{
if (ModelState.IsValid)
{
db.Notifications.Add(notification);
db.SaveChanges();
//NotificationHub obj = new NotificationHub();
//obj.SendNotifications();
}
return View(notification);
}
查看调用jquery事件的位置
@using (Html.BeginForm()){
@Html.ValidationSummary(true)
<div class="publishContainer">
@Html.TextAreaFor(model => model.Text, new { @placeholder = "Enter notification here!!" })
</div>
<p>
<input id="button1" type="button" value="button" />
@section Scripts
{
<script src="~/Scripts/jquery-1.7.1.min.js"type="text/javascript"></script> <script src="~/Scripts/jquery.signalR-1.1.4.js" type="text/javascript"></script> <script src="~/signalr/hubs"></script>
<script type="text/javascript">
$(function () { var notifications = $.connection.notificationHub;// Start the connection.$.connection.hub.start().done(function () { $("#button1").click(function () { notifications.server.sendNotifications(); }).fail(function (e) { alert(e);
});
});
});
</script>
@使用(Html.BeginForm()){
@Html.ValidationSummary(true)
@Html.TextAreaFor(model=>model.Text,新建{@placeholder=“在此处输入通知!!”})
@节脚本
{
$(函数(){var notifications=$.connection.notificationHub;//启动连接。$.connection.hub.Start().done(函数(){$(“#按钮1”)。单击(函数(){notifications.server.sendNotifications()))。失败(函数(e){alert(e);
});
});
});
还有我的主页
$(function () {
var proxy = $.connection.notificationHub;
proxy.client.receiveNotification = function (data) {
for (var i = 0; i < data.length; i++) {
var element = data[i];
$('#discussion').append('<li>' + element.Message + '</li>');
}
$(函数(){
var proxy=$.connection.notificationHub;
proxy.client.receiveNotification=函数(数据){
对于(变量i=0;i”+元素.Message+”);
}
此处通知未保存到数据库。
我想显示新的通知计数(如红色)就像我们在facebook中所做的一样,单击该按钮,我希望展开相同的内容并显示相同的内容。这意味着我希望实现相同的facebook/stacoverflow通知功能。它是facebook中用于通知的div吗???发布代码而不是解释代码更可能得到您的答案。感谢您对代码。这是我在stackoverflow上的第一篇帖子,我自己没能做到。这个问题还不清楚。现在你好像在问“这是facebook上用来通知的div吗?”。但该问题的答案是肯定的/否定的,而提供的代码与此基本无关。请参阅,和。我的问题是如何使用Asp.net MVC实现通知?任何一只手?我对该实现感到厌倦发布您的代码而不是解释它更可能得到您的答案。感谢您格式化代码。这是我的第一次在stackoverflow中发布,我无法自己完成。这个问题仍然不清楚。现在你似乎在问“这是facebook中用于通知的div吗?”.但该问题的答案是肯定的/否定的,而提供的代码基本上与此无关。请参阅,和。我的问题是如何使用Asp.net MVC实现通知?任何人?我对此实现感到困惑