C
C#3y ago
pyrodistic

❔ AJAX MVC Table DatePick

Could someone help me manipulating table results with ajax? I'm using MVC. I was using it before to manipulate results on a combo box, but my front-end is really bad... On Controller/Repository:
//Controller
[HttpPost]
[Route("Appointments/GetVetsAsync")]
public async Task<JsonResult> GetAppointmentsAsync(DateTime date)
{
var dateResult = await _appointmentRepository.GetAppointmentFromDate(date);

return Json(dateResult.OrderBy(s => s.TimeslotId));
}
//REPO
public async Task<List<Appointment>> GetAppointmentFromDate(DateTime date)
{
var appointments = _context.Appointments.Where(appt => (appt.Date == date.Date));

return await appointments.ToListAsync();
}
//Controller
[HttpPost]
[Route("Appointments/GetVetsAsync")]
public async Task<JsonResult> GetAppointmentsAsync(DateTime date)
{
var dateResult = await _appointmentRepository.GetAppointmentFromDate(date);

return Json(dateResult.OrderBy(s => s.TimeslotId));
}
//REPO
public async Task<List<Appointment>> GetAppointmentFromDate(DateTime date)
{
var appointments = _context.Appointments.Where(appt => (appt.Date == date.Date));

return await appointments.ToListAsync();
}
On the View I have the table with a foreach(var item in Model). I want for ajax to replace the model or filter the item in it when the date picker changes. So I have:
$(document).ready(function () {
$("#Date").change(function () {
$("#table_appointments").empty();
$.ajax({
url: '@Url.Action("GetAppointmentsAsync","Appointments")',
type: 'POST',
dataType: 'json',
data: { date: $("#Date").val() },
success: function (appointments) {
$(document).ready(function () {
$("#Date").change(function () {
$("#table_appointments").empty();
$.ajax({
url: '@Url.Action("GetAppointmentsAsync","Appointments")',
type: 'POST',
dataType: 'json',
data: { date: $("#Date").val() },
success: function (appointments) {
I think I'm just missing the syntax for the ajax function, the Model is a IEnumerable so I'm not sure if json is the right dataType in this case. Would appreciate any help!
1 Reply
Accord
Accord3y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server