In my ASP.NET Core MVC app, I am using a jQuery datatable and it uses Ajax calls to load data from database. The datasource is a List<Student>
object.
Now I load the datatable with List<Student>
object. And I remove or add any student object to List<Student>
in the client side, how I can reflect these changes in the datatable?
After removing 1 or 2 items from the List<Student>
, how we can reflect these changes in the datatable without reloading the page again?
I tried with this code:
$('#example').DataTable().ajax.reload(); // nothing happens to datatable
exampletable.ajax.reload(); // nothing happens to datatable
2
Answers
If you’re adding/deleting items in the list from the client side, you should be able to also remove them from the list without the need for a reload. That would be ok from a transactional point of view, until you are sure to make those changes in the underlying data service.
Anyway, you must, somehow, apply those changes in the database before reloading the table from the ajax call. This will also show any external changes to the data on your page.
You can do it, for example, with another ajax call which doesn’t have to update the table immediately.
A whole working demo to achieve your requirement for doing database call after client side add/remove operation:
Model
View
Controller