I don't know what the problem is. My data is being saved, but toast message and redirection to an action method are not working. The fun part is - while debugging, it executes both ending conditions but I don't know why this is not working! Toast messages and redirections are working on other controllers.
Code:
[HttpPost]
public async Task<IActionResult> CreateSlot(AppointmentViewModel model, Appointment appointment)
{
var currentUser = await _tokenService.GetUserViaToken();
string[] timeParts = model.Time.Split('-');
if (timeParts.Length == 2)
{
string startTimeString = timeParts[0].Trim();
string endTimeString = timeParts[1].Trim();
// only start time
TimeOnly startTime = TimeOnly.ParseExact(startTimeString, "hh:mm tt", CultureInfo.InvariantCulture);
TimeOnly endTime = TimeOnly.ParseExact(endTimeString, "hh:mm tt", CultureInfo.InvariantCulture);
var appointmentDate = DateTime.ParseExact(model.Date, "yyyy-MM-dd", CultureInfo.InvariantCulture);
var dateCheck = appointmentDate.DayOfWeek.ToString();
var slotId = _context.AvailableSlots
.Where(x => x.UserId == model.DoctorId
&& x.StartTime == startTime
&& x.EndTime == endTime
&& x.Day.Name == appointmentDate.DayOfWeek.ToString())
.Select(x => x.Id)
.FirstOrDefault();
var data = new Appointment
{
AppointmentById = currentUser.Id,
AppointmentWithId = model.DoctorId,
CreatedTime = DateTime.UtcNow,
AppointmentDate = appointmentDate,
AppointmentStatusId = 1,
AvailableSlotId = slotId
};
_context.Appointments.Add(data);
await _context.SaveChangesAsync();
TempData.AddToastMessage("success", "Appointment Created!");
return RedirectToAction("Index", "Home");
}
else
{
return null;
}
}
I want to show the the toast message and redirect to the Index
view