我已经开发了SignalR集线器类,以通过客户端和服务器传递数据。当预订控制器Hit命中时,我调用了ReservationHub.NotifyCurrentResertiovationToAllClients();
方法,该方法工作正常,但在通过拾取器局部视图的“驱动程序视图”中无效。下面提到我的代码。在这里,我无法在驱动程序视图中调用updatedClients()
方法。
我添加了一个制动点,并确认它在NotifyCurrentResertiovationToAllClients
类的ReservationHub
方法中被击中。
但是它发现没有任何反应。
ReservationHub.cs
namespace Taxi_Project
{
public class ReservationHub : Hub
{
[HubMethodName("NotifyClients")]
public static void NotifyCurrentResertiovationToAllClients()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<ReservationHub>();
context.Clients.All.updatedClients();
}
}
}
HomeController.cs
[HttpGet]
public ActionResult Driver()
{
Pickup();
return View();
}
[HttpGet]
public ActionResult Pickup()
{
List<Reservation> ReseravationList ;
using (DataContext DbContextHelper = new DataContext())
{
ReseravationList = DbContextHelper.Reservations.ToList();
}
return PartialView("_Pickup", ReseravationList);
}
[HttpGet]
public ActionResult Reservation()
{
return View();
}
[HttpPost]
public ActionResult Reservation(Reservation ReservationObj)
{
using (DataContext DbContextHelper = new DataContext())
{
DbContextHelper.Reservations.Add(ReservationObj);
DbContextHelper.SaveChanges();
ReservationHub.NotifyCurrentResertiovationToAllClients();
}
return View();}
ReservationModel类
public class Reservation
{
[Key]
public int ReservationID { get; set; }
[Required]
public string ReservationType { get; set; }
[Required]
public string ReservationDetails { get; set; }
}
皮卡局部视图
@model IEnumerable<Taxi_Project.Models.Reservation>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.ReservationType)
</th>
<th>
@Html.DisplayNameFor(model => model.ReservationDetails)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ReservationType)
</td>
<td>
@Html.DisplayFor(modelItem => item.ReservationDetails)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ReservationID }) |
@Html.ActionLink("Details", "Details", new { id=item.ReservationID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ReservationID })
</td>
</tr>
}
</table>
驱动程序视图
@{
ViewBag.Title = "Driver";
}
<h2>Driver</h2>
@Html.Partial("_Pickup")
@section scripts{
<script src="~/Scripts/jquery.signalR-2.4.0.js"></script>
<script src="~/Scripts/jquery.signalR-2.4.0.min.js"></script>
<script src="~/signalr/hubs"></script>
<script type="text/javascript">
$(function () {
// Create a proxy to signalr hub on web server. It reference the hub.
var notificationFromHub = $.connection.ReservationHub;
//Connect to signalr hub
$.connection.hub.start().done(function () {
FetchEmployees();
});
// Notify to client with the recent updates
notificationFromHub.client.updatedClients = function () {
alert('2');
FetchEmployees();
};
});
function FetchEmployees() {
var model = $('#dataTable');
$.ajax({
url: '/home/Pickup',
contentType: 'application/html ; charset:utf-8',
type: 'GET',
dataType: 'html'
})
.success(function (result) { model.empty().append(result); })
}
</script>
}