我正在开发一个asp.net core 2.2 Web应用程序,这是第一次使用数据表。我阅读了有关该主题的所有教程,并从Github实施了一个教程(已为我的应用程序对其进行了修改),但是一周以来,没有任何内容使它显示SQL数据库表中的任何数据。
namespace SUCOCoreControl.Controllers
{
public class RazorBudgetController : Controller
{
private readonly SUCODbContext _context;
public RazorBudgetController(SUCODbContext context)
{
_context = context;
}
// GET: /<controller>/
public IActionResult OPEXWBS()
{
return View();
}
[HttpPost]
public IActionResult LoadData()
{
try
{
var draw = HttpContext.Request.Form["draw"].FirstOrDefault();
// Skiping number of Rows count
var start = Request.Form["start"].FirstOrDefault();
// Paging Length 10,20
var length = Request.Form["length"].FirstOrDefault();
// Sort Column Name
var sortColumn = Request.Form["columns[" + Request.Form["order[0][column]"].FirstOrDefault() + "][name]"].FirstOrDefault();
// Sort Column Direction ( asc ,desc)
var sortColumnDirection = Request.Form["order[0][dir]"].FirstOrDefault();
// Search Value from (Search box)
var searchValue = Request.Form["search[value]"].FirstOrDefault();
//Paging Size (10,20,50,100)
int pageSize = length != null ? Convert.ToInt32(length) : 0;
int skip = start != null ? Convert.ToInt32(start) : 0;
int recordsTotal = 0;
// Getting all data
var wbsData = (from tempwbs in _context.OPEXWBS
select tempwbs);
//Sorting
if (!(string.IsNullOrEmpty(sortColumn) && string.IsNullOrEmpty(sortColumnDirection)))
{
wbsData = _context.OPEXWBS.OrderBy(sortColumn + " " + sortColumnDirection);
}
//Search
if (!string.IsNullOrEmpty(searchValue))
{
wbsData = wbsData.Where(m => m.OPEXWBSID.Contains(searchValue) || m.OPEXWBSENG.Contains(searchValue) || m.SubHeaderID.Contains(searchValue));
}
//total number of rows count
recordsTotal = wbsData.Count();
//Paging
var data = wbsData.Skip(skip).Take(pageSize).ToList();
//Returning Json Data
return Json(new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = data });
}
catch (Exception)
{
throw;
}
}
<table id="wbsTable" class="table display nowrap table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>SubHeaderID</th>
<th>OPEXWBSID</th>
<th>OPEXWBSENG</th>
<th>ServiceMaterialENG</th>
<th>ServiceMaterialARB</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
</div>
@section Scripts {
<script>
$(function () {
$('#wbsTable').DataTable({
"dom": 'Bfrtip',
"buttons": [
'pageLength',
'copyHtml5',
'excelHtml5',
'csvHtml5',
'pdfHtml5'
],
"select": true,
"processing": true, // for show progress bar
"serverSide": true, // for process server side
"filter": true, // this is for disable filter (search box)
"orderMulti": false, // for disable multiple column at once
"ajax": {
"type": 'POST',
//"url": 'LoadData',
"url": "@Url.Action("LoadData", "RazorBudget")",
"dataSrc": "",
"datatype": "jason",
"contentType": 'application/json',
},
"columnDefs":
[{
"targets": [0],
"visible": false,
"searchable": false
}],
"columns": [
{ "data": "SUBHeaderID", "name": "SUBHeaderID", "autoWidth": true },
{ "data": "OPEXWBSID", "name": "OPEXWBSID", "autoWidth": true },
{ "data": "ServiceMaterialENG", "name": "ServiceMaterialENG", "autoWidth": true },
{ "data": "ServiceMaterialARB", "name": "ServiceMaterialARB", "autoWidth": true },
{
"render": function (data, type, full, meta)
{ return "<a href='#' class='btn btn-info' onclick=Edit('" + full.OPEXWBSID + "'); >Edit</a>"; }
},
{
data: null, render: function (data, type, row)
{
return "<a href='#' class='btn btn-danger' onclick=Delete('" + row.OPEXWBSID + "'); >Delete</a>" ;
}
},
]
});
});
function DeleteData(OPEXWBSID)
{
if (confirm("Are you sure you want to delete ...?"))
{
Delete(OPEXWBSID);
}
else
{
return false;
}
}
function Delete(OPEXWBSID)
{
var url='@Url.Content("~/")' + "/Delete" ;
$.post(url, { ID: OPEXWBSID }, function (data)
{
if (data)
{
oTable=$('#wbsTable').DataTable();
oTable.draw();
}
else
{
alert("Something Went Wrong!");
}
});
}
</script>
}
Response Headers
HTTP/2.0 200 OK
cache-control: no-cache, no-store
pragma: no-cache
content-type: text/html; charset=utf-8
server: Kestrel
x-sourcefiles: =?UTF-8?B?QzpcVXNlcnNcbW9hdGFcc291cmNlXHJlcG9zXFNVQ09Db3JlQ29udHJvbFxTVUNPQ29yZUNvbnRyb2xccmF6b3JidWRnZXRcb3BleHdicw==?=
x-powered-by: ASP.NET
date: Mon, 25 Mar 2019 09:06:57 GMT
X-Firefox-Spdy: h2
页面和表的标题(标题)正在加载,但表主体和数据未显示。
可能会出现渲染问题?还是ajax脚本中的“ URL”部分?