在表单asp.net mvc5中添加两个按钮?

时间:2017-07-20 12:45:20

标签: asp.net-mvc

我正在尝试向页面添加两个按钮,一个“接受”按钮和一个“拒绝”按钮。我这样做了

 <button type="submit" name="SubmitButton" value="Accept" class="btn btn-sm btn-success">Accept</button>
    <button type="submit" name="SubmitButton" value="Reject" class="btn btn-sm btn-danger">Reject</button>

然后在我的控制器中我有以下

public ActionResult ViewRequest(string SubmitButton, int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            RequestLift requestLifts = db.requestLifts.Find(id);

            if (requestLifts == null)
            {
                return HttpNotFound();
            }
            Lift lifts = db.Lifts.Find(id);

            if (SubmitButton == "Accept")
            {
                requestLifts.bookingStatus = "Accepted";
                db.SaveChanges();

                return RedirectToAction("Index", "Home");
            }
            else if (SubmitButton == "Reject")
            {
                requestLifts.bookingStatus = "Rejected";
                db.SaveChanges();t request has been rejected";
                return RedirectToAction("Index", "Home");
            }

            return View(requestLifts);
        }

然而按钮点击没有任何反应?

    @Html.BeginForm("ViewRequest", "SubmitButton")
    {
    <button type="submit" name="SubmitButton" value="Accept" class="btn btn-sm btn-success">Accept</button>
    <button type="submit" name="SubmitButton" value="Reject" class="btn btn-sm btn-danger">Reject</button>
}

1 个答案:

答案 0 :(得分:-1)

您需要使用Html.BeginForm打包using。这决定了如何添加开始和结束表单标签,即在表单中的按钮周围。您提交的问题是这些按钮目前实际上不在您的表单标记内。

@using (Html.BeginForm("ViewRequest", "SubmitButton"))
{
    <button type="submit" name="SubmitButton" value="Accept" class="btn btn-sm btn-success">Accept</button>
    <button type="submit" name="SubmitButton" value="Reject" class="btn btn-sm btn-danger">Reject</button>
}