javascript - 未使用 ajax 从 Jquery 对话框调用 Controller 后操作

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

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

我认为的 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;
        });

在我的 Controller 中发布操作:

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

局部 View :

@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>
}

最佳答案

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

局部 View :

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

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

关于javascript - 未使用 ajax 从 Jquery 对话框调用 Controller 后操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44042467/

相关文章:

javascript - 是否可以通过文件上传上传 JSON 文件并在不与 API 交谈的情况下读取 Angular 6 中的内容?

javascript - jQuery/Javascript - 如何在按钮的值更改时触发事件?

java - 如何根据单击的单选按钮在新表中显示值?

jquery - 根据 "media screen"值调用外部JS文件

c# - 将字符串列表传递给由 asp 代码后面的方法发送的 javascript

javascript - 发送 JSON stringify 到 PHP 如何读取它

javascript - Sinon - 带有回调的 stub 函数 - 导致测试方法超时

javascript - 转发器行突出显示不会在回发后持续存在

ajax - jQuery AJAX JSON 数据类型转换

php - Codeigniter 查询更新所有行而不是 1