如何从jQuery重定向到POST ActionResult?

时间:2017-05-12 04:50:22

标签: javascript jquery asp.net asp.net-mvc

我尝试在代码中添加toastr通知。在添加它之前,它完美地工作。但是当我尝试在jQuery验证后添加toastr通知时,它显示记录已成功执行但它不会像以前一样重定向或保存数据。似乎jQuery没有将viewModel数据转发给POST方法。以下是我的代码:

@model ESportsScreening.ViewModel.ScreeningFormViewModel

@{
    ViewBag.Title = "New Screening ";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>New Screening</h2>

@using (Html.BeginForm("Create", "Screenings", FormMethod.Post, new { id = "createForm" }))
{
    <p class="alert alert-info">All fields are <strong>required</strong></p>
    @Html.AntiForgeryToken()
    <div class="form-group">
        @Html.LabelFor(m => m.TeamA)
        @Html.TextBoxFor(m => m.TeamA, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.TeamA)
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.TeamB)
        @Html.TextBoxFor(m => m.TeamB, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.TeamB)
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Date)
        @Html.TextBoxFor(m => m.Date, new { @class = "form-control", placeholder = "eg 1 Jan 2018" })
        @Html.ValidationMessageFor(m => m.Date)
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Time)
        @Html.TextBoxFor(m => m.Time, new { @class = "form-control", placeholder = "eg 24:00" })
        @Html.ValidationMessageFor(m => m.Time)
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Ticket)
        @Html.TextBoxFor(m => m.Ticket, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.Ticket)
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Competition)
        @Html.DropDownListFor(m => m.Competition, new SelectList(Model.Competitions, "Id", "Name"), "", new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.Competition)
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Description)
        @Html.TextAreaFor(m => m.Description, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.Description)
    </div>

    <button class="btn btn-primary">Save</button>
}

@section scripts
{
    @Scripts.Render("~/bundles/jqueryval")

    <script>
        $(document).ready(function () {
            $("#createForm").submit(function (e) {
                e.preventDefault();
                $('#createForm').validate();

                if ($('#createForm').valid()) {
                    toastr.success("Screening recorded succesfully!");
                } else {
                    toastr.fail("Something unexpected happened..");
                }
            });
        });
    </script>
}

我的jQuery需要进行哪些更改才能将我转发到ScreeningController和Create(viewModel)的POST方法?

1 个答案:

答案 0 :(得分:2)

您需要删除 publicDB.perform(query, inZoneWith: nil) { (results, error) -> Void in 。它阻止在服务器上发布。

e.preventDefault();