Я разработал класс-концентратор SignalR для передачи данных через клиент и сервер.Когда контроллер резервирования нажал, я вызвал метод 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>
}