JavaScript按钮不会转到HttpPost方法

时间:2016-07-05 15:56:37

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

JS:

<script type="text/javascript">
    $('#dialog').dialog({
        autoOpen: false,
        width: 600,
        modal: true,
        buttons: {
            "Yes": function () {
                $("#DSCreateForm").submit(); // part that isn't working correctly
            },
            "Cancel": function () { $(this).dialog("close"); }
        }
    });

    var hobbsValue = $('#HobbsValue').val();

    alert(hobbsValue);

    if (hobbsValue == null || hobbsValue.length == 0) {
        $('.btnSubmitDS').on("click", function (e) {
            e.preventDefault();

            $(function () {
                $('#dialog').dialog('open')
            });

        });
    }
</script>

CSHTML:

@using (Html.BeginForm("Create", "DailySummaries", FormMethod.Post, new { id = "DSCreateForm" }))

<div class="form-group">
        @Html.LabelFor(model => model.Hobbs, "HOBBS:", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Hobbs, new { htmlAttributes = new { id = "HobbsValue", @class = "form-control hobbs-textbox", @placeholder = "xxxx.x" } })
            @Html.ValidationMessageFor(model => model.Hobbs, "", new { @class = "text-danger" })
        </div>
    </div>

控制器:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "...")] DailySummary dailySummary)
    {

在上面的代码中,当选择“是”按钮时,它正在提交但是当我调试时它不会进入控制器的HttpPost方法。我该怎么做呢?

1 个答案:

答案 0 :(得分:0)

我正在大胆地假设你没有设定 method="post" 在你的表格上?

编辑:现在你已经提供了表单创建代码,我可以看到情况并非如此。