简体   繁体   中英

Refresh table when new record is inserted in ASP.NET Core MVC

I'm creating an app with ASP.NET Core MVC, Razor, EF Core, SQL Server as the database.

I have 3 views: List , Create , and Edit .

  • List - showing the data from the table (as a list)
  • Create - for creating a new record
  • Edit - to edit a selected record

These pages will be accessed by different users.

The case is: how can I update the list without refreshing the List page? So when there's a new record, the data.table will be updated with a new record, even when the record is edited.

I am trying the code with this sample --> https://github.com/dyatchenko/ServiceBrokerListener

But it seems that the page didn't updated.

My code:

public IActionResult Index()
{
    var connectionString = this.Configuration.GetConnectionString("SQLConnection");
    var listener = new SqlDependencyEx(connectionString, "ProductStatistic", "TrialPurpose", listenerType: SqlDependencyEx.NotificationTypes.Insert);
    listener.TableChanged += (o, e) => Console.WriteLine("Your table was changed!");
    listener.Start();

    var model = _db.TrialPurposeDataViews.FromSqlRaw("exec spTrialPurposeDataView").ToList();
    listener.Stop();

    return View(model);
}

Did I make a mistake on the code?

Need advice and help, really appreciated.

Thank you.

How can I update the list without refreshing the List page?

Do you mean that you want to see record changes in near-real-time in your web page, without requiring an entire postback / page refresh?

You need to use client side programming for that, and the only client side language that works in browsers is Javascript. MS are pushing something new called "Blazor" for client side programming, but it's not really "native" as far as I can tell.

The functionality you're after is delivered using something called "AJAX" which is basically "Using javascript to make your web page behave like a thick client application"

There are now many javascript plugins, libraries, frameworks and layers that do what you want. I recommend you buy a third party tool rather than write your own. I've seen Telerik used successfully in the past.

It's not very well explained in various ASP.Net MVC articles that there is no way to do all that "standard" stuff if just using ASP.NET MVC.

If you do take the AJAX / Telerik route, keep in mind that you still need to build API's in your controller. These basically return data, often in JSON format, which your client side javascript uses to update the page in-place rather than having to post back.

An option that I tried and gave up on was the free jqGrid libray. Here's an example of how incredibly complicated it is to get seemingly simple things like grid updates running.

Real-time data in a grid - better method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM