单击一次按钮更改布尔值的状态

时间:2018-01-25 22:51:04

标签: javascript c# jquery asp.net

为约会开发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>

我整天都在参加这个项目,所以可能很累的眼睛在和我玩耍

1 个答案:

答案 0 :(得分:1)

看起来你大部分都在那里。但是您的提交按钮需要在一个将再次发回的表单中。

这是一种方法,它可以发布到特定的&#34; ConfirmApppointment&#34;服务器上的动作方法将确认约会(因为这似乎是您想要更新的唯一字段,因此不需要回发整个约会模型)。

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

详细信息视图将是这样的:

public static class UsersExtensionMethods
{
    public static UserVm ToVm(this User user)
    {
        return new UserVm
        {
            Name = user.Name,
            Age = user.Age,
            Location = user.Location
        };
    }
}

// by name
public IEnumerable<UserVm> GetUsersByName (string name){
      return db.Users.Where(x=>x.Name == name).Select(u => u.ToVm()).Tolist();
}

// by age
public IEnumerable<UserVm> GetUsersByAge (int age){
      return db.Users.Where(x=>x.Age == age).Select(u => u.ToVm()).Tolist();
}

// by age
public IEnumerable<UserVm> GetUsersByLocation (string location){
      return db.Users.Where(x=>x.Location== location).Select(u => u.ToVm()).Tolist();
}

在您的控制器中(我假设您的控制器被称为&#34;约会&#34;,但如果不是,请在Html.BeginForm中修改它):

<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>
}
相关问题