【问题标题】:How to close dialog after posting JsonResponse success in MVC在 MVC 中发布 JsonResponse 成功后如何关闭对话框
【发布时间】:2017-08-05 07:30:40
【问题描述】:

通过单击保存按钮发布 Dialog Ajax 表单后,我无法仅关闭 Dialog 并返回到原始页面。

相反,我在新页面中显示了 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>
}

【问题讨论】:

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


    【解决方案1】:

    我发现我不见了

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

    【讨论】:

      猜你喜欢
      • 2018-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多