2016-06-25 8 views
2

Ich bin total verwirrt, was das genaue Problem ist. es ist nicht Datensatz aktualisieren, bis ich meine Seite aktualisieren ....Datenbank Änderung SignalR Echtzeit-Feedback

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(); 
      } 
     } 

    } 

hier mein Tabellenschema ...

Bewertungen

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; } 
    } 

hier ist meine controlle r Code

public ActionResult GetFeedback() 
     { 
      FeedbackRepository _feedbackRepository = new FeedbackRepository(); 


      return PartialView("_feedbackList", _feedbackRepository.GetAllMessages()); 
     } 

hier ist die Teilansicht ==>

_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> 

hier ist mein feedbackHub Code ... feedbackHUb

public class FeedbackHub: Hub { private statische Zeichenfolge conString = ConfigurationManager.ConnectionStrings ["conn"]. ToString(); public void Hallo() { Clients.All.hello(); }

[HubMethodName("sendMessages")] 
public static void SendMessages() 
{ 
    IHubContext context = GlobalHost.ConnectionManager.GetHubContext<FeedbackHub>(); 
    context.Clients.All.updateMessages(); 
} 

} Ansicht Code hier == >>>

<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> 

ich habe auch notwendigen Code in startup.cs Klasse und in Global.asax Datei hinzugefügt.

Anmerkung: ** Ich habe die aktiviert ** Service-Broker: ...

ich kann nicht fein, was das genau Problem ist ...

Antwort

2

ich denke, Ihr Problem ist in Ihrem Code anzeigen ...

var notifications = $.connection.messagesHub; 

messageHub ist nicht Ihr Hub Sie dies mit Ihrem eigenen Hub unter

wie angegeben ersetzen sollte
var notifications = $.connection.feedbackHub; 

Hoffnung dies wird Ihr Problem

+0

danke löse ich mit Ihrer Hilfe gelöst ... – sajiii

+0

Sie sind willkommen –