Datatables Server Side: How to fetch data from database using Entity Framework instead of dummy data
Datatables Server Side: How to fetch data from database using Entity Framework instead of dummy data
Sifat Ahmed
Posts: 3Questions: 1Answers: 0
I am following this article https://levelnis.co.uk/blog/datatables-with-web-api-part-3-paging-sorting-searching where dummy data is used using a class.
I am stuck in here (Here I want to fetch data from database using Entity Framework)
public class CustomerSearchController : SearchController
{
public IHttpActionResult Post(SearchRequest request)
{
var allCustomers = JsonConvert.DeserializeObject<CustomerData>(CustomerData.DataSource);
var response = WrapSearch(allCustomers.Data, request);
return Ok(response);
}
private static CustomerSearchResponse WrapSearch(ICollection<CustomerSearchDetail> details, SearchRequest request)
{
var results = ApiHelper.FilterCustomers(details, request.Search.Value).ToList();
var response = new CustomerSearchResponse
{
Data = PageResults(results, request),
Draw = request.Draw,
RecordsFiltered = results.Count,
RecordsTotal = details.Count
};
return response;
}
}
CustomerData Class:
public class CustomerData
{
public const string DataSource = @"
{
""Data"": [
{
""CompanyName"": ""Microsoft"",
""Address"": ""1 Microsoft Way, London"",
""Postcode"": ""N1 1NN"",
""Telephone"": ""020 7100 1000""
},
{
""CompanyName"": ""Nokia"",
""Address"": ""2 Nokia Way, London"",
""Postcode"": ""N2 2NN"",
""Telephone"": ""020 7200 2000""
},
{
""CompanyName"": ""Apple"",
""Address"": ""3 Apple Way, London"",
""Postcode"": ""N3 3NN"",
""Telephone"": ""020 7300 3000""
}
]
}";
public IList<CustomerSearchDetail> Data { get; set; }
}
I don't want to use dummy constant data. Instead, I want the data to come from ApplicationDbContext using Entity Framework. Any help?
This discussion has been closed.
Answers
Sorry - that article wasn't written by myself, so you'd need to contact the author if you need help with it. I've not used EF much, so I can't help in that regard.
Allan
Thank you so much for your reply.
I have contacted the author. I guess he is busy. Let see.