title: Issues with functionality of getting the right user to be saved - displays another user instead (Sqlite. EF Asp.Net)
body: <p>When I log in as a patient I have an issue. The main issue is they can create an appointment, validation shows that the appointment has been created. However, it will not display on their profile. I then logged in as the other users and noticed that it does display but the wrong patient name it has displayed for. Therefore If I am a specific patient when logged in, it will create the appointment for another patient instead.</p>
  <p>Prior to asking this question I have done the following.</p>
  <p>I have logged in from two different users as a manager and admin. They can create an appointment for a patient by selecting the patient's name and the doctor's name. This gets saved to the database (SQLite, EF). When checking where this new entry is saved and being displayed. I have logged in as the doctor, patient, and admin. It displays fine on all the master views (Dashboard or profiles of the patient, doctor, index, etc).</p>
  <p>I then attempted to go to the patient's profile as a manager and create the appointment that way. This is fine too, it will display everything fine.</p>
  <p>I have a feeling it has something to do with the controller but I think the logic is correct. If anyone knows how to solve this or the issue that I am having I would be very grateful.</p>
  <p>Controller</p>
  <pre><code> // GET: PatientController/CreateAppointment
      [Authorize(Roles=&quot;Manager,Doctor,Patient&quot;)]
      public IActionResult CreateAppointment(int id)
      {
          // patient can only create appointment for themselves
          if (User.IsInRole(&quot;Patient&quot;))
          {
              id = GetSignedInUserId();
          }
         
          var p =  svc.GetPatientById(id); 
  
          if (p == null)
          {
              Alert(&quot;No such Patient exists&quot;, AlertType.warning); //Letting the user know the booking does not exist and remoded Id to be displayed
              return RedirectToAction(nameof(Index)); // return to the index
          }
          
          //Create Appointment booking and populate the Patient Id 
          var ab = new AppointmentViewModel { 
              PatientId = p.Id, 
              DoctorSelectList = new SelectList( svc.GetAllDoctors(), &quot;Id&quot;, &quot;Name&quot;)
          };
          return View(ab);
      }
  
      //Post PatientController// AppointmentBooking
      [HttpPost]
      [Authorize(Roles=&quot;Manager,Doctor,Patient&quot;)]
      [ValidateAntiForgeryToken] // support writes a unique value to an HTTP-only cookie and then the same value is written to the form. When the page is submitted, an error is raised if the cookie value doesn't match the form value.
      public IActionResult CreateAppointment(AppointmentViewModel ab)
      {
          // verify the appointment slot is available for this doctor
          if (!svc.IsAppointmentSlotForDoctorAvailable(ab.DoctorId, ab.StartDateTimeBooking))
          {
              ModelState.AddModelError(&quot;ApptDate&quot;, &quot;Appointment Slot is not available&quot;);
          }
          //Check the returned Patient is not null and if Alert created to inform the user
          if (ModelState.IsValid)
          {
              // create appointment from view model and pass to service for storage
              var appointment = new Appointment {
                  StartDateTimeBooking = ab.StartDateTimeBooking,
                  PreferenceOfAttendence = ab.PreferenceOfAttendence,
                  DetailOfBooking = ab.DetailOfBooking,
                  PatientId = ab.PatientId,
                  DoctorId = ab.DoctorId
              };
              svc.AddAppointment(appointment);
      
              Alert($&quot;Booking is created&quot;, AlertType.success); //Notify the user that the appointment booking was created!
  
              if (User.IsInRole(nameof(Role.Manager)))
              {
                  // display patient dashboard
                  return RedirectToAction(nameof(Index));
              }
              else if (User.IsInRole(nameof(Role.Admin)))
              {
                  return RedirectToAction(nameof(Index));
              }
              else
              {
                  return RedirectToAction(nameof(Dashboard), new { Id = ab.PatientId }); // return user back to index
              }
          }
          
          Alert($&quot;Appointment Booking is not created!&quot;, AlertType.warning); //Alerting the user the appointment booking was not created
          
          ab.DoctorSelectList = new SelectList( svc.GetAllDoctors(), &quot;Id&quot;, &quot;Name&quot;);
          return View(ab); //return the appointment booking to the view 
      }
  </code></pre>
  <p>This is my view</p>
  <pre><code>&lt;div class=&quot;row row card shadow p-4 mt-4&quot;&gt;
  &lt;div class=&quot;col-md-4&quot;&gt;
      &lt;form asp-action=&quot;CreateAppointment&quot;&gt;
          &lt;div asp-validation-summary=&quot;ModelOnly&quot; class=&quot;text-danger&quot;&gt;&lt;/div&gt;
          &lt;input type=&quot;hidden&quot; asp-for=&quot;PatientId&quot; class=&quot;form-control&quot; /&gt;
          &lt;div class=&quot;form-group&quot;&gt;
              &lt;label asp-for=&quot;ApptDate&quot; class=&quot;control-label&quot;&gt;&lt;i class=&quot;bi bi-calendar&quot;&gt; &lt;/i&gt;&lt;strong&gt;Booking&lt;/strong&gt;&lt;/label&gt;
              &lt;div class=&quot;d-flex&quot;&gt;
                  &lt;input type=&quot;time&quot; asp-for=&quot;ApptTime&quot; class=&quot;form-control&quot; value=&quot;@Model.ApptTime&quot; /&gt;
                  &lt;input type=&quot;date&quot; asp-for=&quot;ApptDate&quot; class=&quot;form-control&quot; value=&quot;@Model.ApptDate&quot; /&gt;
              &lt;/div&gt;
              &lt;span asp-validation-for=&quot;ApptDate&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
          &lt;/div&gt;
          &lt;div class=&quot;form-group mt-3&quot;&gt;
              &lt;label asp-for=&quot;DoctorId&quot; class=&quot;control-label&quot;&gt;&lt;i class=&quot;bi-person-badge-fill&quot;&gt;&lt;/i&gt; &lt;strong&gt;Doctor&lt;/strong&gt;&lt;/label&gt;
              &lt;select asp-for=&quot;DoctorId&quot; class=&quot;form-control&quot; asp-items=&quot;@Model.DoctorSelectList&quot;&gt;
                  &lt;option&gt;Select Doctor&lt;/option&gt;
              &lt;/select&gt;
              &lt;span asp-validation-for=&quot;DoctorId&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
          &lt;/div&gt;
          &lt;div class=&quot;form-group mt-3&quot;&gt;
              &lt;label asp-for=&quot;PreferenceOfAttendence&quot; class=&quot;control-label&quot;&gt;&lt;i class=&quot;bi-person-badge-fill&quot;&gt;&lt;/i&gt; &lt;strong&gt;Preference of Attendance&lt;/strong&gt;&lt;/label&gt;
              &lt;select asp-for=&quot;PreferenceOfAttendence&quot; asp-items=&quot;Html.GetEnumSelectList&lt;PreferenceOfAttendance&gt;()&quot; class=&quot;form-control&quot;&gt;
                  &lt;option&gt;Select choice of appointment&lt;/option&gt;
              &lt;/select&gt;
              &lt;span asp-validation-for=&quot;PreferenceOfAttendence&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
          &lt;/div&gt;
          &lt;div class=&quot;form-group&quot;&gt;
              &lt;label asp-for=&quot;DetailOfBooking&quot; class=&quot;control-label&quot;&gt; &lt;strong&gt;Reason For booking&lt;/strong&gt;&lt;/label&gt;
              &lt;textarea rows=&quot;5&quot; asp-for=&quot;DetailOfBooking&quot; class=&quot;form-control&quot;&gt;&lt;/textarea&gt;  &lt;!------ Text box created here--&gt;
              &lt;span asp-validation-for=&quot;DetailOfBooking&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
          &lt;/div&gt;
  
          &lt;br /&gt;
  
          &lt;div asp-roles=&quot;Admin, Manager&quot; class=&quot;form-group&quot;&gt;
              &lt;input type=&quot;submit&quot; value=&quot;Create&quot; class=&quot;btn btn-primary&quot; /&gt;
              &lt;a class=&quot;btn btn-secondary&quot; asp-action=&quot;index&quot; asp-route-id=@Model.PatientId&gt;Cancel&lt;/a&gt;
          &lt;/div&gt;
          &lt;div asp-roles=&quot;Patient&quot; class=&quot;form-group&quot;&gt;
              &lt;input type=&quot;submit&quot; value=&quot;Create&quot; class=&quot;btn btn-primary&quot; /&gt;
              &lt;a class=&quot;btn btn-secondary&quot; asp-action=&quot;Dashboard&quot; asp-route-id=@Model.PatientId&gt;Cancel&lt;/a&gt;
          &lt;/div&gt;
  
      &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Services database</p>
  <pre><code>       public Appointment AddAppointment(Appointment ab)
      {
          // you need to retrieve patient using the PatientId foreign key.
          var patient = GetPatientById(ab.PatientId);
  
          // if patient does NOT exist then cannot create appointment
          if (patient == null)
          {
              return null;
          }
      
          db.Appointments.Add(ab); //Add new booking to the db
          db.SaveChanges(); //Savechanges and return to the db 
  
          // return booking 
          
  </code></pre>