0

我正在返回一个 json 数据,我可以确认它正在将数据带回客户端。但不是更新我的 jqueryaccordion,而是要求我保存或打开文件。下面是我的脚本和控制器。我已经使用 jquery 模态对话框通过部分视图编辑员工详细信息,单击更新按钮应该更新手风琴列表中的相应员工。任何帮助将不胜感激 - 谢谢

更新

通过 IE 工具调试时,我注意到单击“更新”按钮时,“请求”选项卡中的“启动器”显示“单击”。我猜这应该是 'XMLHttpRequest' 而不是。希望这些信息有所帮助。谢谢

主视图

@Html.ActionLink("Edit Employee", "EditEmployees", "Home",
                        new { id = item.Id }
                        , new { @class = "editLink" })

带有编辑员工表单的部分视图 - EditEmployee.cshtml

@using (Ajax.BeginForm("EditEmployees", "Home", new AjaxOptions
        {
            InsertionMode = InsertionMode.Replace, 
            HttpMethod = "POST",
            OnSuccess = "updateSuccess"
        }, new { @id = "updateEmployeeForm" }))
    {
        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>
    </fieldset>
}

返回包含 editemployee 表单的部分视图的操作结果

Public ActionResult EditEmployee(int id)
{
//DataAccess part
return PartialView("EditEmployee",employeedata);
}

更新员工详细信息后返回 Json Result 的控制器

[HttpPost]
Public JsonResult EditEmployee(Models.Employee employee)  
{  
       //Data access part

     JsonResult result = new JsonResult();
                            result.Data = employeeData;
                            return result;
}

主视图上的脚本

   <script type="text/javascript">
            var linkObj;
            $(function () {
                $(".editLink").button();

            $('#updateDialog').dialog({
                autoOpen: false,
                width: 400,
                resizable: false,
                modal: true,
                buttons: {
                    "Update": function () {
                        $("#update-message").html(''); 
                        $("#updateEmployeeForm").submit();
                    },
                    "Cancel": function () {
                        $(this).dialog("close");
                    }
                }
            });

            $(".editLink").click(function () {
                //change the title of the dialog
                linkObj = $(this);
                var dialogDiv = $('#updateDialog');
                var viewUrl = linkObj.attr('href');
                $.get(viewUrl, function (data) {
                    dialogDiv.html(data);

                    var $form = $("#updateEmployeeForm");
                    // Unbind existing validation
                    $form.unbind();     
                               dialogDiv.dialog('open');
                });
                return false;
            });

        });

 function updateSuccess(data) {
 // I want to make sure that this function gets executed on Success instead of a file being sent back to me from the server
  $('#updateDialog').dialog('close');
  $('#commonMessage').html("Update Complete");
  $('#commonMessage').delay(400).slideDown(400).delay(3000).slideUp(400);
  alert("hello");

        }
4

2 回答 2

1
$.get(viewUrl, function (data) {

如果这是您获取ajax数据的方式,我可以说,此服务器代码返回错误:

JsonResult result = new JsonResult();
    result.Data = employeeData;
    return result;

因为默认情况下JsonResult只允许POST请求

作为解决方案,您可以使用JsonRequestBehavior.AllowGet

于 2013-04-01T12:21:30.737 回答
1

Might be a bit late but ... adding the jquery unobtrusive reference to my view fixed the issue.

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")"></script>
于 2017-07-17T02:05:34.073 回答