2017-08-05 71 views
0

通過單擊保存按鈕發佈對話Ajax表單後,我無法關閉對話框並返回到原始頁面。如何在MVC中發佈JsonResponse成功後關閉對話框

相反,我得到了在新頁面中顯示的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> 
} 

回答

0

我發現我失蹤

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