asp.net mvc - Database update notification using SignalR and Entity Framework -
i'm trying make website using asp.net mvc 4
, ef 6
want update messagebox of adminpanel if user sends him message. i'm using signalr
live notification i'm new signalr
, reason don't updates live, i've refresh page newly added message. here codes,
hub class
public class chathub : hub { myproj.models.msgtoowner messages = new myproj.models.msgtoowner(); public void sendnotify(string username, string title, string datetimenow) { var newname = messages.name; var newtitle = messages.title; var newtime = messages.addedtime; ihubcontext context = globalhost.connectionmanager.gethubcontext<chathub>(); context.clients.all.getnotify(newname, newtitle, newtime); } }
view messages
@{ var msglist = (list<myproj.models.msgtoowner>)viewbag.msglist; } <body> <script src="~/scripts/jquery.signalr-1.1.4.min.js"></script> <script src="~/signalr/hubs"></script> <script src="~/scripts/livescript.js"></script> <ul class="nav navbar-top-links navbar-right"> <li class="dropdown"> <a class="dropdown-toggle" data-toggle="dropdown" href="#"> <i class="fa fa-envelope fa-fw"></i><i class="fa fa-caret-down"></i> </a> <ul class="dropdown-menu dropdown-messages" id="newitem"> @foreach (var item in msglist) { <li> <a href="#"> <div> <strong>@html.displayfor(modelitem => item.title)</strong> <span class="pull-right text-muted"> <em>@html.displayfor(modelitem => item.addedtime)</em> </span> </div> <div>sent : <strong>@html.displayfor(modelitem => item.name)</strong></div> </a> </li> <li class="divider"></li> } </ul> </li> </ul> </body>
livescript.js (script updating signalr)
$(function () { var chat = $.connection.chathub; chat.client.getnotify = function (username, title, datetimenow) { $('#newitem').append( '<li><a href="#"><div><strong>' + htmlencode(title) + '</strong><span class="pull-right text-muted"><em>' + htmlencode(datetimenow) + '</em></span></div><div>sent : <strong>' + htmlencode(username) + '</strong></div></a></li><li class="divider"></li>'); }; $.connection.hub.start().done(function () { $('#btnsend').click(function () { chat.server.sendnotify($('#getname').val(), $('#gettitle').val(), $('#getdate').val()); }); }); });
i must doing wrong here can't figure out since i'm new signalr
. how can update messagebox whenever new message saved in db? guidance helpful me. thanks.
Comments
Post a Comment