2
votes

i am totally confused what the exactly problem is. it is not update record until i refresh my page....

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

    }

here is my table schema ...

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

here is my controller code

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


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

here is the partial view ==>

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

here is my feedbackHub code... feedbackHUb

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

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

} view code here==>>>

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

i have also added necessary code in startup.cs class and in Global.asaxfile.

note:**i have enabled the **service broker:...

i can't fine what the exactly problem is...

1

1 Answers

2
votes

i think your problem is in your view code ...

 var notifications = $.connection.messagesHub;

messageHub is not your hub you should replace this with your own hub like given below

var notifications = $.connection.feedbackHub;

hope this will solve your problem