我完全困惑到底是什么问题。在刷新我的页面之前它不是更新记录....
码==>>> 的 FeedbackRepository
public class FeedbackRepository
{
readonly string _connString =
ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
public IEnumerable<Feedback> GetAllMessages()
{
var messages = new List<Feedback>();
using (var connection = new SqlConnection(_connString))
{
connection.Open();
using (var command = new SqlCommand(@"SELECT [FeedbackID],
[email], [subject], [message] FROM [dbo].[Feedbacks]", connection))
{
command.Notification = null;
var dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
var reader = command.ExecuteReader();
while (reader.Read())
{
messages.Add(item: new Feedback
{
FeedbackID = (int)reader["FeedbackID"],
email = (string)reader["email"],
subject = reader["subject"] != DBNull.Value ?
(string)reader["subject"] : "",
message =(string) reader["message"]
});
}
}
}
return messages;
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
FeedbackHub.SendMessages();
}
}
}
这是我的表架构......
反馈
public class Feedback
{
[Required]
[Key]
public int FeedbackID { get; set; }
public string email { get; set; }
[Required]
public string subject { get; set; }
[Required]
public string message { get; set; }
}
这是我的控制器代码
public ActionResult GetFeedback()
{
FeedbackRepository _feedbackRepository = new FeedbackRepository();
return PartialView("_feedbackList", _feedbackRepository.GetAllMessages());
}
这是局部视图==&gt;
_feedbacksList
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.subject)
</th>
<th>
@Html.DisplayNameFor(model => model.message)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.subject)
</td>
<td>
@Html.DisplayFor(modelItem => item.message)
</td>
<td>
@Html.ActionLink("Delete", "Delete", new { id = item.email })
</td>
</tr>
}
</table>
这是我的feedbackHub代码... 的 feedbackHUb
public class FeedbackHub:Hub { private static string conString = ConfigurationManager.ConnectionStrings [&#34;康恩&#34;]的ToString(); public void Hello() { Clients.All.hello(); }
[HubMethodName("sendMessages")]
public static void SendMessages()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<FeedbackHub>();
context.Clients.All.updateMessages();
}
} 在此处查看代码==&gt;&gt;&gt;
<div id="messagesTable"></div>
<script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
<script src="/signalr/hubs"></script>
<script type="text/javascript">
$(function () {
// Declare a proxy to reference the hub.
var notifications = $.connection.messagesHub;
//debugger;
// Create a function that the hub can call to broadcast messages.
notifications.client.updateMessages = function () {
getAllMessages()
};
// Start the connection.
$.connection.hub.start().done(function () {
alert("connection started")
getAllMessages();
}).fail(function (e) {
alert(e);
});
});
function getAllMessages()
{
var tbl = $('#messagesTable');
$.ajax({
url: '/Home/GetFeedback',
contentType: 'application/html ; charset:utf-8',
type: 'GET',
dataType: 'html'
}).success(function (result) {
alert("connection started")
tbl.empty().append(result);
}).error(function () {
});
}
</script>
我还在startup.cs
类和Global.asax
文件中添加了必要的代码。
注意:**我启用了**服务经纪人:...
我无法解决确切的问题......
答案 0 :(得分:2)
我认为您的问题出在您的观看代码中......
var notifications = $.connection.messagesHub;
messageHub不是您的中心,您应该将其替换为您自己的集线器,如下所示
var notifications = $.connection.feedbackHub;
希望这将解决您的问题