2016-03-23 158 views
0

我正在使用以下脚本并成功从数据库中删除底层记录。但点击确认后,根本没有任何反应。这意味着我必须关闭两个弹出窗口,然后在浏览器上点击F5以查看结果。我尝试过很多事情,但必须有一些简单的我失踪这里JQUERY不关闭弹出窗口和刷新页面

function deletePayment(customerPaymentId) { 
    //alert(customerPaymentId); 
    bootbox.confirm("Are you sure? This payment will be logically deleted", function (result) { 
     if (result) { 
      var url = '/CustomerPayment/Delete'; 
      var data = { 
       id: customerPaymentId 
      }; 

      $.post(url, data, function() { 

       window.location.reload(); 
      }); 



     } 
    }); 
    return false; 
} 

控制器下面的代码:

//[HttpPost, ActionName("Delete")] 
//[ValidateAntiForgeryToken] 

//[Authorize(Roles = "Delete")] 
public ActionResult Delete(int id) 
{ 

    CustomerPayment obj = _db.GetCustomerPayment(id); 

    _db.Edit(obj); 

    obj.TransactionDateTimeEnd = DateTime.Now; 
    _db.Save(); 




    return View("Index", new { id = obj.CustomerId}); 
} 
+0

取出返回false?你确定你还包括Jquery吗? – KyleK

+0

查看$ .post的jquery文档:http://api.jquery.com/jquery.post/ 您只为$ .post实现'success'处理程序,所以如果该帖子失败(不以200(OK)状态码返回),窗口永远不会重新加载。 –

+0

如果你只是打算调用'window.location.reload();'来重新加载页面,那么没有必要使用ajax。 –

回答

0

下面是正确的代码,我已经测试。

我会告诉你删除返回false并使下面的ajax调用明确附加成功和失败回调。

$(document).on("click", ".alert", function (e) { 
      bootbox.confirm("Delete payment ???", function (result) { 
        if (result) { 
         alert('delete fired'); 
         $.ajax({ 
          type: "GET", 
          url: "Jquery-datatable.aspx/GetJsonEmps", 
          data: "{}", 
          dataType: "json", 
          contentType: "application/json; charset=utf-8", 
          success: function (response) { 
           alert('ok'); 
           location.reload(); 
          }, 
          error: function (xhr, ajaxOptions, thrownError) 
          { 
          alert(xhr.responseText); 
          location.reload(); 
          }  
         }); 
        } 
       }); 
      }); 

注:我已迅速例如上述检查testing.You起见必须更换rquest类型POST和请求url并添加data参数。

0

对于控制器

public JsonResult Delete(int? id) 
     {   
      if (isSuccess) 
       return Json(new { Success = true, Message = "" }, JsonRequestBehavior.AllowGet); 
      else 
       return Json(new { Success = false, Message = error }, JsonRequestBehavior.AllowGet); 
     } 

和你的脚本中添加以下行

window.location.href = window.location.href; 

让我知道如果你有任何问题。

相关问题