Javascript 单击按钮后更改布尔值的状态

Javascript 单击按钮后更改布尔值的状态,javascript,c#,jquery,asp.net,Javascript,C#,Jquery,Asp.net,为约会开发C应用程序。我希望管理员能够进入详细信息页面,并单击按钮确认约会,这是一个布尔值设置为false开始。单击后,我将重新加载页面,确认约会 我很难理解如何做到这一点 这是我的控制器: [HttpPost] [ValidateAntiForgeryToken] public ActionResult Details([Bind(Include = "AppointmentId,Confirmed")] Appointments appointment

为约会开发C应用程序。我希望管理员能够进入详细信息页面,并单击按钮确认约会,这是一个布尔值设置为false开始。单击后,我将重新加载页面,确认约会

我很难理解如何做到这一点

这是我的控制器:

    [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);
        }
以及我想在其中完成此部分的详细信息页面:

<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>

我在这个项目上干了一整天,所以可能是疲惫的眼睛在玩弄着我

看起来你已经完成了大部分工作。但是您的提交按钮需要位于一个表单中,该表单将再次发回

这里有一种方法,它将发布到服务器上的特定ConfirmAppPoint操作方法,该方法将确认约会,因为这似乎是您要更新的唯一字段,所以不需要真正回发整个约会模型

我认为您还需要将约会ID放在一个隐藏字段中,以便将其发回服务器,以便知道要确认的约会:

详细信息视图如下所示:

<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>
}

您的javascript/jquery代码在哪里,我看到了这里的标记,您在使用此代码时面临什么问题?
//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);
}