There is a site on ASP.Net mvc. The site displays the status of multiple devices. Devices write regardless of the site values ​​in the database (mysql). The page is a hodgepodge. In View there is one connected model, but this model contains a list of all objects. For example:

public class SyObVM { // список городов public IEnumerable<CVM> Cities { get; set; } //сообщения public IEnumerable<Ev> Events { get; set; } таких коллекций несколько } 

All these collections are collected in the controller and transferred to the View.

It is necessary to implement the following scheme: The user logged in, a page was displayed, a certain table was checked once every n seconds, whether something new appeared there, if the information was added / changed, it is necessary to update the whole page and signal it to the user.

In the database previously made a trigger that writes to another table, the time and the last identifier.
I don’t know how in asp net mvc to compare the old state with the new one from the database and so on each cycle and accordingly interact with the view.

If the application were desktop, I remembered the value at the start, then every n seconds I would take the current one from the required table, compare the values, if I change, notify and change the starting one, if not, continue ...

Probably need a bunch of JS / Ajax .. but experience on the web is not enough.

  • The check should occur on the client side i. in the browser, which means, which means you cannot do without JS. - Bulson
  • But in the controller it can not be done? for example, in js I call the controller every 10 seconds, so setInterval (function () {....}, 10,000) ;. and the controller is already analyzing ... - Denis Ivanov
  • List, add a javascript function in the view that searches for the last id entry on the page and makes a POST request for ajax action, passing this last id value and appending the new lines that came to the page. You won't even need a trigger in the database. Do you have a classic mvc or core? - AK
  • I have a classic mvc. example js is not there? I want to make a post request and pass the values ​​there, as I know, but I don’t know how to find the last id and append the incoming lines. - Denis Ivanov

1 answer 1

Concept Since you are not really on js, I made you a simple example on an old jquery.

We will have one simple model:

 namespace WebApplication2.Models { public class Order { public int Id { get; set; } public string Title { get; set; } } } 

And a demo controller with list output and diff output:

 public class OrderController : Controller { public ActionResult Index() { var model = new List<Order> { new Order { Id = 1, Title = "First order" }, new Order { Id = 2, Title = "Second order" }, }; return this.View(model); } public ActionResult GetNewRecords(int maxId) { var model = new List<Order> { new Order { Id = maxId + 1, Title = $"MaxId was {maxId}" }, }; return this.Json(model); } } 

(Fill in a call from the database yourself)

And this is actually the view itself:

 @model List<WebApplication2.Models.Order> @{ ViewBag.Title = "Order monitor"; } <h2>@ViewBag.Title</h2> <table id="orders" class="table"> <tr> <td>Id</td> <td>Title</td> </tr> @foreach (var order in Model) { <tr> <td data-id="@order.Id">@order.Id</td> <td>@order.Title</td> </tr> } </table> <script type="text/javascript"> var timeout = setInterval(reloadRecords, 5000); function reloadRecords () { $.ajax({ type: "POST", url: "@Url.Action("GetNewRecords", "Order")", data: { "maxId": getMaxId() }, success: function (data) { $(data).each(function(index, item) { $(`<tr><td data-id="${item.Id}">${item.Id}</td><td>${item.Title}</td><tr>`).appendTo('#orders'); }); } }); } function getMaxId () { var max = 0; $('#orders tr td:first-child').each(function() { max = Math.max($(this).data("id") || 0, max); }); return max; } </script> 

View after a couple of cycles:

enter image description here

  • Thank you very much. - Denis Ivanov