如何在MVC中发布JsonResponse成功后关闭对话框

时间:2017-08-05 07:30:41

标签: jquery asp.net json ajax asp.net-mvc

点击保存按钮发布Dialog Ajax表单后,我无法关闭对话框并返回原始页面。

相反,我在新页面中显示了Json响应。

我知道我错过了一些但不知道在哪里看

我的代码是

Index.cshtml

<div id="dialog-model" title="Basic Model Dialog">
</div>

<script>
    $(function() {
        $("#dialog-model").dialog({
            autoOpen: false,
            width: 900,
            height: 450,
            show: {
                effect: "blind",
                duration: 1000,
            },
            hide: {
                effect: "blind",
                duration: 1000,

            }
        });
    });

    function editAccount(accountId) {
        $("#dialog-model").dialog("open");
        $.ajax({
            url: '/Account/Edit',
            contentType: 'application/html',
            data: {id: accountId},
            success: function(content) {
                $('#dialog-model').html(content);
            },
            error: function(e) {}
        });
    };
    function updateSuccess(data) {
        alert(data);
    }
</script>

AccountController.cs

// GET: Account Edit
public ActionResult Edit(int id)
{
    var account = _accountService.GetAccountById(id);

    return PartialView(account == null ? new AccountDetailsModel(new Account()) : new AccountDetailsModel(account));
}

// POST: Account Edit
[HttpPost]
public ActionResult Edit(AccountDetailsModel accountDetailsModel)
{
    if (ModelState.IsValid)
    {
        //return RedirectToAction("Index");
        return Json(new { success = true, responseText = "The attached file is not supported." }, JsonRequestBehavior.AllowGet);
    }
    else
    {
        return Json(new { success = false, responseText = "The attached file is not supported." }, JsonRequestBehavior.AllowGet);
    }
}

Edit.cshtml

@model BusinessOracle.Web.Models.Accounts.AccountDetailsModel


@using (Ajax.BeginForm("Edit", "Account", new AjaxOptions
{
    InsertionMode = InsertionMode.Replace, 
    HttpMethod = "POST",
    OnSuccess = "updateSuccess"
}, new { @id = "edit-account-form" }))
{
    @Html.ValidationSummary(true)
    <div id="update-message" class="error invisible"></div>
    <fieldset>
        <legend>Edit Account</legend>

        @Html.HiddenFor(model => model.Id)

        <div class="editor-label">
            @Html.LabelFor(model => model.Code)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Code)
            @Html.ValidationMessageFor(model => model.Code)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Description)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Description)
            @Html.ValidationMessageFor(model => model.Description)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

1 个答案:

答案 0 :(得分:0)

我发现我失踪了

<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>