【问题标题】:Submit form not hitting action, instead opening new page提交表单不执行操作,而是打开新页面
【发布时间】:2020-04-09 15:18:29
【问题描述】:

仅供参考,我还使用 jQuery 从部分页面填充字段 model.VdiId。当我单击表单页面的提交按钮时,它会将我重定向到一个空白页面,而不是点击 Booking 控制器的 BookVdi 操作。我抛出异常并放置没有命中的断点。

以下是查看代码:

@model HVDI.Models.BookingViewModel

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}


@using (@Html.BeginForm("BookVdi", "Booking", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary()
    <h4>Booking</h4>
    <hr />

    <div class="form-inline">


        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.VdiId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.VdiId, new { htmlAttributes = new { @class = "form-control disabled" } })
                @Html.ValidationMessageFor(model => model.VdiId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.BookingDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.BookingDate, new { htmlAttributes = new { @class = "form-control datepicker" } })
                @Html.ValidationMessageFor(model => model.BookingDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.TimeStart, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.TimeStart, new { htmlAttributes = new { @class = "form-control timepicker" } })
                @Html.ValidationMessageFor(model => model.TimeStart, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.TimeEnd, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.TimeEnd, new { htmlAttributes = new { @class = "form-control timepicker" } })
                @Html.ValidationMessageFor(model => model.TimeEnd, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="showing">
            <input type="button" class="align-content-end" onclick="" id="btnSearch" value="Search" />
        </div>

    </div>
    <br />
    <div class="form-group">
        <div class="table-dark" id="vdiSection">

        </div>
    </div>

    <br />
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Book" class="btn btn-default" />
        </div>
    </div>

}

以下是控制器代码:

public ActionResult Index()
        {
            return View();
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        public void BookVdi(BookingViewModel bookingDetails)
        {
            if (!ModelState.IsValid)
            {
                throw new Exception();
            }
                Booking newBooking = new Booking();
            newBooking.BookingDate = bookingDetails.BookingDate; //I am putting a breakpoint here
        }

它正在打开一个空白页面: http://localhost:61110/Booking/BookVdi

【问题讨论】:

  • 同时显示你的 jQuery 代码。
  • 不确定“打开空白页”是什么意思。 MVC 中没有“页面”。 Booking/BookVdi 是一个控制器动作。也可能值得展示 HttpGet 方法的控制器代码。
  • 检查 F12 网络选项卡 - 查找您的 POST 并检查详细信息以验证它是否发布了您期望的数据。另外,在你的控制器 POST 方法的第一行放置一个断点,并验证你没有遇到你抛出的异常。
  • 这让我更困惑。添加控制器代码

标签: c# asp.net-mvc forms view


【解决方案1】:

您会看到空白页面,因为正确执行操作时您可能不会返回任何内容。请按以下方式更改您的代码。

    [HttpPost]
    [ValidateAntiForgeryToken]
    public void BookVdi(BookingViewModel bookingDetails)
    {
        if (!ModelState.IsValid)
        {
            ViewBag,Error = "Please, correct all errors.";
            return View("YOURVIEWNAMEFOR FORM");
        }
        Booking newBooking = new Booking();
        newBooking.BookingDate = bookingDetails.BookingDate; 
        // Write save code here
        return RedirectToAction("Index");  // you must redirect to something when action executed.
    }

还添加此 ViewBag 以查看视图中的错误

@Html.AntiForgeryToken()
@Html.ValidationSummary()
@ViewBag.Error             // add this line
<h4>Booking</h4>

如果您需要在视图question 中显示特定的模型错误,也请查看此问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 1970-01-01
    相关资源
    最近更新 更多