【问题标题】:Controller post action is not being called from Jquery dialog using ajax没有使用 ajax 从 Jquery 对话框调用控制器发布操作
【发布时间】:2017-10-17 22:56:30
【问题描述】:

我正在尝试使用 ajax 从 jquery 对话框调用我的控制器的 post 操作方法,但无法做到这一点。我确信有一些非常小的东西,但即使在多次检查我的代码后也找不到任何东西。有人可以帮我找出问题吗?

在我看来是Javascript:

$('a.actionLinkAttentionButton').click(function () {
            var url = $(this).attr('href');
            var dialogDiv = $('<div id="success" style="display:none;background-color:#efeeef;"></div>').appendTo('body');
            dialogDiv.load(url, {},
                function (responseText, textStatus, XMLHttpRequest) {
                    dialogDiv.dialog({
                        title: "Resolve log issue",
                        close: function (event, ui) {
                            dialogDiv.remove();
                        },
                        width: 600,
                        show: { effect: "blind", duration: 400 },
                        hide: { effect: "blind", duration: 500 },
                        modal: { backdrop: 'static', keyboard: false },
                        buttons: {
                            "Save": function () {
                                $.ajax({
                                    url: '@Url.Action("ResolveLogPost","Attendance")',
                                    type: "POST",
                                    async: false,
                                    data: $('ResolveLogForm', $(this)).serialize()
                                });
                            },
                            "Close": function () {
                                $(this).dialog('close');
                            },
                        },
                        success: function (response) {
                            dialogDiv.dialog('close');
                        },
                        dialogClass: 'no-close success-dialog'
                    });
                });
            return false;
        });

在我的控制器中发布操作:

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ResolveLogPost(BiometricLog model)
        {
            return PartialView();
        }

局部视图:

@using (Html.BeginForm("ResolveLogPost", "Attendance", FormMethod.Post, new { enctype = "multipart/form-data", @id = "ResolveLogForm" }))
{
    @Html.AntiForgeryToken()

    <fieldset>
        <legend>BiometricLogAction</legend>

        <table class="ListTable" style="width:100%">
            @Html.HiddenFor(model => model.EmployeeCode)
            @Html.HiddenFor(model => model.date)
            <tr><td colspan="2" style="background-color:#a6c100;color:white">@Html.Label("Log Details")</td></tr>
            <tr>
                <td style="width:200px;">@Html.LabelFor(model => model.LogDate)</td>
                <td>@Html.DisplayFor(model => model.LogDate)</td>
            </tr>
            <tr>
                <td>@Html.LabelFor(model => model.FinalInTime)</td>
                <td>@Html.DisplayFor(model => model.FinalInTime)</td>
            </tr>
            <tr>
                <td>@Html.LabelFor(model => model.FinalOutTime)</td>
                <td>@Html.DisplayFor(model => model.FinalOutTime)</td>
            </tr>
            <tr>
                <td>@Html.LabelFor(model => model.WorkingTime)</td>
                <td>@Html.DisplayFor(model => model.WorkingTime)</td>
            </tr>
        </table>
        <table>
            <tr>
                <td>@Html.CheckBoxFor(model => model.Accepted, new { @id = "chkprivate" })</td>
                <td>@Html.LabelFor(model => model.Accepted)</td>
            </tr>
        </table>
        <table>
            <tr>
                <td>@Html.LabelFor(model => model.ProofFileName)</td>
                <td>@Html.TextBoxFor(m => m.ProofFileName, new { type="file", name = "file", @id="uploadfile"})</td>
            </tr>
            <tr>
                <td>@Html.LabelFor(model => model.Remarks)</td>
                <td>@Html.TextAreaFor(model => model.Remarks, new { style = "width:300px;" })</td>
            </tr>
        </table>
    </fieldset>
}

【问题讨论】:

  • 你不能只用data: $("ResolveLogForm").serialize()吗?并指定发送的dataType 是什么? (json)
  • 已经试过但还是不行
  • 在控制器参数上使用FromBody 属性怎么样? public ActionResult ResolveLogPost([FromBody]BiometricLog model)
  • 还是没有运气:(
  • 好的,发送带有预期参数名称的数据怎么样? data: { "model": $('ResolveLogForm', $(this)).serialize() }

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


【解决方案1】:

经过几次更改后终于可以正常工作了。虽然,我真的不相信这些真的是原因的根源,但仍然很高兴它成功了;)。我做了以下更改:

局部视图:

  1. 添加了空路由值以使用不同的重载
  2. 将 enctype 更改为 @enctype

    @using (Html.BeginForm("ResolveLogPost", "Attendance", null, FormMethod.Post, new { @id = "form", @enctype = "multipart/form-data" }))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-10
    • 2012-11-30
    • 2020-10-13
    • 2017-03-26
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多