我试图在view.api中将数据从api加载到我的数据表中,所以我使用了ajax并将表ID传递给datatable。我在api和模型中都有一个属性Name,但是我不知道为什么在运行代码时出现此错误。这是与我的问题有关的主要部分:
<table id="Customers" class=" table-hover"> @*table table-bordered*@
<thead>
<tr>
<th>name</th>
<th>Discount Rate</th>
<th>Delete </th>
</tr>
</thead>
<tbody>
</tbody>
</table>
@section scripts
{
<script>
$(document).ready(function () {
$("#Customers").DataTable({
ajax: {
url: "/api/customer",
dataSrc: ""
},
columns: [
{
data: "Name",
render: function (data, type, customers) {
return "<a href='/customer/edit/" + customers.CustomerID + ">" + data + "</a>"
}
},
{
data: "Name"
},
{
data: "CustomerID",
render: function (data, type, Customers)
{
return "<button clasee='btn-link js-delete' data-customer-id=" + Customers.CustomerID + ">delete</button>"
}
}
]
});
...
这是我的api类:
public class CustomerDto
{
public int CustomerID { get; set; }
[Required]
[StringLength(255)]
public string Name { get; set; } //here is name property!
[MinAge18Requierd]
public DateTime? birthday { get; set; }
public bool IsSubscribedToNewsletter { get; set; }
public byte MembershipTypeID { get; set; }
}
这是api控制器:
public class CustomerController : ApiController
{
private IdentityModels _context;
public CustomerController()
{
_context = new IdentityModels();
}
public IEnumerable<CustomerDto> GetCustomer()
{
var customer = _context.customers.ToList().Select(Mapper.Map<Customers, CustomerDto>);
return customer;
}
public CustomerDto GetCustomer(int id)
{
var Customer = _context.customers.SingleOrDefault(m => m.CustomerID == id);
if (Customer == null)
throw new HttpResponseException(HttpStatusCode.NotFound);
return Mapper.Map<Customers,CustomerDto>(Customer);
}
[HttpPost]
public CustomerDto CreateCustomer(CustomerDto customerDto)
{
if (!ModelState.IsValid)
throw new HttpResponseException(HttpStatusCode.BadRequest);
var customer= Mapper.Map<CustomerDto, Customers>(customerDto);
_context.customers.Add(customer);
_context.SaveChanges();
customerDto.CustomerID = customer.CustomerID;
//customerDto.birthday = customer.birthday;
//customerDto.Name = customer.Name;
//customerDto.IsSubscribedToNewsletter = customerDto.IsSubscribedToNewsletter;
//customerDto.MembershipTypeID = customer.MembershipTypeID;
return customerDto;
}
[HttpPut]
public void UpdateCustomer(int id,CustomerDto customerDto)
{
if (!ModelState.IsValid)
{
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
var _Customer = _context.customers.SingleOrDefault(m => m.CustomerID == id);
if (_Customer==null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
Mapper.Map(customerDto,_Customer);
//_Customer.Name = customer.Name;
//_Customer.MembershipTypeID = customer.MembershipTypeID;
//_Customer.IsSubscribedToNewsletter = customer.IsSubscribedToNewsletter;
//_Customer.birthday = customer.birthday;
//_context.SaveChanges();
}
[HttpDelete]
public void DeleteCustomer(int id)
{
if (!ModelState.IsValid)
{
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
var _Customer = _context.customers.SingleOrDefault(m => m.CustomerID == id);
if (_Customer == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
_context.customers.Remove(_Customer);
_context.SaveChanges();
}
}
我收到此错误:
DataTables warning: table id=Customers - Requested unknown parameter 'Name' for row 0, column 1. For more information about this error
我不明白为什么会出现此错误。 TIA
答案 0 :(得分:0)
经过大量研究,我发现问题与这部分有关:
columns: [
{
data: "Name",
render: function (data, type, customers) {
return "<a href='/customer/edit/" + customers.CustomerID + ">" + data + "</a>"
}
我了解使用驼峰表示法时,必须向数据传递以小写字母开头的参数。这意味着我的代码只能在这里更改:
data: "name" //instead of "Name"
因为我的datasrc返回了骆驼符号格式的json对象列表。 希望这篇文章可以对遇到这种问题的人有所帮助。