-1

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

3
  • What version of .NET?
    – Richard
    Commented Jul 7 at 12:55
  • 1
    Also please be very explicit in the description of what happens: what do you mean by "both ending conditions"?
    – Richard
    Commented Jul 7 at 12:58
  • Please make your question clear, do you mean sometimes enter if sometimes else? Is tempdata null or redirection fails or both not working? You could debug on return redirection to see if the data stored. If the same method works on other controllers, please give a minimal reproducible example so that we can compare and check them. Meanwhile , is the way you call the endpoint different from others? Commented Jul 8 at 3:33

0

Browse other questions tagged or ask your own question.