Я пытаюсь загрузить данные из API в мою таблицу данных в view. Поэтому я использовал ajax и передаю свой идентификатор таблицы в datatable.У меня есть свойство Name как в API, так и в модели, но я не знаю, почему я получаю эту ошибку, когда запускаю свой кодЭто основная часть, которая относится к моей проблеме:
<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
Я не понимаю, почему я получаю эту ошибку.ТИА