Anna Doe
Anna Doe

Reputation: 47

Change the state of a boolean once button is clicked

Developing a C# application for appointments. I want the admin to be able to go into the details page and click a button to confirm an appointment, which is a boolean set to false to start. Once its clicked, I'll have the page to reload with the appointment confirmed.

I'm having trouble understanding how to achieve this.

Here's my controller:

    [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Details([Bind(Include = "AppointmentId,Confirmed")] Appointments appointments)
        {
            if (ModelState.IsValid)
            {
                db.Entry(appointments).State = EntityState.Modified;
                db.SaveChanges();
return RedirectToAction("Details", new { id = appointments.AppointmentId });
            }

            return View(appointments);
        }

And my details page where I want to do this part:

<th>
        @Html.DisplayName("Appointment Status")
        </th>
        @if (Model.Confirmed == false)
        { 
        <td>
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Confirm Appointment" class="btn btn-default" />
                </div>

            </div>
        </td>
        }
        else if (Model.Confirmed == true)
        {
            <td>
                @Html.DisplayName("Appointment Confirmed")
            </td>
        }
    </tr>

I've been at this project all day so maybe tired eyes are playing with me

Upvotes: 0

Views: 1796

Answers (1)

ADyson
ADyson

Reputation: 61904

Looks like you're most of the way there. But your submit button needs to be within a form which will post back again.

Here's one way to do it, where it posts to a specific "ConfirmApppointment" action method on the server which will confirm the appointment (since that appears to be the only field you want to update, there's no real need to post back the whole appointment model).

I think you also need to put the appointment ID in a hidden field so it gets posted back to the server in order to know which appointment to confirm:

Details view will be something like this:

<th>Appointment Status</th>
@if (Model.Confirmed == false)
{ 
  <td>
  @using (Html.BeginForm("ConfirmAppointment", "Appointment", FormMethod.Post))
  {
    <div class="form-group">
      <div class="col-md-offset-2 col-md-10">
        <input type="submit" value="Confirm Appointment" class="btn btn-default" />
        <input type="hidden" name="appointmentID" value="@Model.Id"/>
      </div>
    </div>
  }
  </td>
}
else if (Model.Confirmed == true)
{
  <td>Appointment Confirmed</td>
}

And in your controller (I have assumed your controller is called "Appointments", but if not please amend it in the Html.BeginForm) above:

//this method is just for displaying your Details view. I'm not sure how exactly your code gets to here, since you didn't specify much (apart from a button gets clicked), so if the method signature is wrong, that'll be something for you to fix yourself, or ask another question about
public ActionResult Details(Appointments appointment)
{
    return View(appointment);
}

//this method receives the postback when the "Confirm Appointment" button is pressed, and updated the appointment details. Then it returns to the user to the same screen, where this time they should see the "Appointment Confirmed" message instead of the button
[HttpPost]
public ActionResult ConfirmAppointment(int appointmentID)
{
  var appt = db.Appointments.Find(appointmentID);
  appt.Confirmed = true;
  db.SaveChanges();
  return View(appt);
}

Upvotes: 1

Related Questions