Hi @Jagjit Saini ,
When loading data by Ajax (ajax), DataTables by default, expects the data loaded to be valid JSON.
- You did not give all the code, for example, how do you use DataTables to load data, so I can only speculate, you did not use ajax to load data.
- use ajax to load data
var table = $('#tblLocation').DataTable({ ajax: { url: "[@](/users/na/?userId=1a465025-7ffe-0003-0000-000000000000).Action("getallLocations","Home")", dataSrc: 'objLocation' }, columns: [ { data: "Id" }, { data: "Description" }, { data: "IsActive" } ] });
- use ajax to load data
- You can check the ajax.reload() method, you will find that it is used to reload the table data from the Ajax data source.
- table.ajax.reload();
- I wrote an example based on the code you provided, and you can check it out.
- You only need to change the method of querying data and the method of adding data to the database in my example to your own.
Model
public class Location
{
[Key]
public string Id { get; set; }
public string Description { get; set; }
public string IsActive { get; set; }
}
Controller
public class HomeController : Controller
{
public DailyMVCDemoContext db = new DailyMVCDemoContext();
public ActionResult Index()
{
return View();
}
public ActionResult getallLocations()
{
var data = db.Locations.ToList();
return Json(new { objLocation = data },JsonRequestBehavior.AllowGet);
}
public ActionResult Add()
{
return PartialView("_AddView");
}
[HttpPost]
public ActionResult Add(Location model)
{
db.Locations.Add(model);
db.SaveChanges();
return Json("", JsonRequestBehavior.AllowGet);
}
}
Index
<button class="btn btn-primary" id="Addbtn">add</button>
<table class="table" id="tblLocation">
<thead>
<tr>
<th>
Id
</th>
<th>
Description
</th>
<th>
IsActive
</th>
</tr>
</thead>
</table>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button id="saveAdd" type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
JavaScript on Index View
-You need to reference the dataTables.min.js file.
var table = $('#tblLocation').DataTable({
ajax: {
url: "@Url.Action("getallLocations","Home")",
dataSrc: 'objLocation'
},
columns: [
{ data: "Id" },
{ data: "Description" },
{ data: "IsActive" }
]
});
$("body").on("click", "#Addbtn", function () {
$.ajax({
url: "@Url.Action("Add", "Home")",
type: "Get",
success: function (result) {
$(".modal-body").html(result);
$("#myModal").modal("show");
}
});
});
$("body").on("click", "#saveAdd", function () {
var objLocation = {
Id: $('#Id').val().toUpperCase(),
Description: $('#Description').val().toUpperCase(),
IsActive: $('#IsActive').val().toUpperCase()
};
$.ajax({
url: "@Url.Action("Add", "Home")",
data: JSON.stringify(objLocation),
type: "POST",
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (result) {
table.ajax.reload();
$("#myModal").modal("hide");
}
});
});
_AddView Partial View
@model WebApplication8.Models.Location
@Html.DisplayNameFor(m => m.Id)
@Html.TextBoxFor(m => m.Id, new { @class = "form-control" })
@Html.DisplayNameFor(m => m.Description)
@Html.TextBoxFor(m => m.Description, new { @class = "form-control" })
@Html.DisplayNameFor(m => m.IsActive)
@Html.TextBoxFor(m => m.IsActive, new { @class = "form-control" })
Result
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards,
YihuiSun