Je suis totalement confus quel est exactement le problème. il est pas mise à jour dossier jusqu'à ce que je rafraîchir ma page ....Changement de base de données SignalR feedback en temps réel
Code == >>> 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();
}
}
}
voici mon schéma de la table ...
Feedbacks
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; }
}
voici mon controlle r Code
public ActionResult GetFeedback()
{
FeedbackRepository _feedbackRepository = new FeedbackRepository();
return PartialView("_feedbackList", _feedbackRepository.GetAllMessages());
}
est ici la vue partielle ==>
de _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>
voici mon code feedbackHub ... feedbackHUb
classe publique FeedbackHub: Hub { chaîne statique privée conString = ConfigurationManager.ConnectionStrings ["conn"]. ToString(); public void Bonjour() { Clients.All.hello(); }
[HubMethodName("sendMessages")]
public static void SendMessages()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<FeedbackHub>();
context.Clients.All.updateMessages();
}
} code de la vue ici == >>>
<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>
j'ai également ajouté le code nécessaire dans startup.cs
classe et dans le fichier Global.asax
.
Note: ** i ** ont permis au courtier de ...
je ne peux pas bien le problème est exactement ...
merci i résolu avec votre aide ... – sajiii
vous êtes les bienvenus –