【发布时间】:2021-02-23 16:44:26
【问题描述】:
这是我的文件.js
var dataTable;
$(document).ready(function () {
loadDataTable();
});
function loadDataTable() {
dataTable = $('#tblData').DataTable({
"ajax": {
"url": "/admin/category/GetAll",
"type": "GET",
"datatype": "json"
},
"columns": [
{ "data": "name", "width": "50%" },
{ "data": "displayOrder", "width": "20%" },
{
"data": "id",
"render": function (data) {
return `<div class="text-center">
<a href="/Admin/category/Upsert/${data}" class='btn btn-success text-white' style='cursor:pointer; width:100px;'>
<i class='far fa-edit'></i> Edit
</a>
<a onclick=Delete("/Admin/category/Delete/${data}") class='btn btn-danger text-white' style='cursor:pointer; width:100px;'>
<i class='far fa-trash-alt'></i> Delete
</a>
</div>
`;
}, "width": "30%"
}
],
"language": {
"emptyTable": "No records found."
},
"width": "100%"
});
}
当我尝试编译时,我收到一条消息
我检查了控制器代码,没有看到任何附加组件我还附上了 Index.cshtml 文件:
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<br />
<div class="border backgroundWhite">
<div class="row">
<div class="col-6">
<h2 class="text-primary">Category List</h2>
</div>
<div class="col-6 text-right">
<a asp-action="Upsert" class="btn btn-primary"><i class="fas fa-plus"></i> Create New Category</a>
</div>
</div>
<br />
<br />
<table id="tblData" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Display Order</th>
<th></th>
</tr>
</thead>
</table>
</div>
@section Scripts{
<script src="~/js/category.js"></script>
}
这是我的文件控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Uplift.DataAccess.Data.Repository.IRepository;
using Uplift.Models;
using Uplift.Utility;
namespace Uplift.Areas.Admin.Controllers
{
[Area("Admin")]
public class CategoryController : Controller
{
private readonly IUnitOfWork _unitOfWork;
public CategoryController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public IActionResult Index()
{
return View();
}
public IActionResult Upsert(int? id)
{
Category category = new Category();
if (id == null)
{
return View(category);
}
category = _unitOfWork.Category.Get(id.GetValueOrDefault());
if (category == null)
{
return NotFound();
}
return View(category);
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Upsert(Category category)
{
if (ModelState.IsValid)
{
if (category.Id == 0)
{
_unitOfWork.Category.Add(category);
}
else
{
_unitOfWork.Category.Update(category);
}
_unitOfWork.Save();
return RedirectToAction(nameof(Index));
}
return View(category);
}
#region API CALLS
[HttpGet]
public IActionResult GetAll()
{
return Json(new { data = _unitOfWork.Category.GetAll() });
//return Json(new { data = _unitOfWork.SP_Call.ReturnList<Category>(SD.usp_GetAllCategory,null) });
}
[HttpDelete]
public IActionResult Delete(int id)
{
var objFromDb = _unitOfWork.Category.Get(id);
if (objFromDb == null)
{
return Json(new { success = false, message = "Error while deleting." });
}
_unitOfWork.Category.Remove(objFromDb);
_unitOfWork.Save();
return Json(new { success = true, message = "Delete successful." });
}
#endregion
}
}
尝试在 chrome 上的应用程序中添加新类别时,我找不到解决此问题的方法,如果您有任何关于如何解决问题的提示,则“tblData”存在问题。
【问题讨论】:
-
您访问过消息中提到的网站吗?看起来那里有很多信息。
-
你有 "[Area("Admin")]" 这应该是响应的根。我在回复中没有看到“管理员”。
标签: c# ajax asp.net-core razor