2015-06-02 53 views
0

我有一個功能,其中基於條件我顯示jQuery對話框。如果條件爲false,只需要使用刷新重新加載重定向到另一個視圖。 它正常工作jQuery對話框和加載部分視圖。MVC4:跳過AJAX成功,並重定向到另一個視圖從型號

但是,當條件失敗我是RedirectToAction(「索引」),這是假設加載新鮮。但是在jQuery對話框中再次加載。這意味着Ajax回撥成功回撥。

我的問題是如何避免jquery ajax成功回調並完全跳過。

這裏是代碼。

$.ajax({ 
     url: 'SaveAddress', 
     type: 'Post', 
     cache: false, 
     data: $("#frmAddress").serialize(), 
     success: function (data) { 

      $("#dialogPopSuggestions").dialog({ 
       autoOpen: false, 
       height: 600, 
       width: 500, 
       modal: true, 
       title: "Suggesion Addresses", 
       //dialogClass: 'warning-dialog', 
       open: function (event, ui) { 
        //Load the action which will return 
        // the partial view 
        $(this).html(data); 
        event.preventDefault(); 
        event.stopPropagation(); 
       } 
      }); 
      $("#dialogPopSuggestions").dialog("open"); 
     }, 
     error: function (xhr, ajaxOptions, thrownError) { 
      alert("error occured"); 
     } 

    [HttpPost] 
    public ActionResult SaveAddress(FormCollection col) 
    { 
      .... 
      .... 
      if (addresssuggestionList.Count == 0) //no suggestion 
     { 
      memberAddressDetails = memberAddress.AddAddress(memberId, memberAddressDetails); 
     } 
     else 
     { 
      addresssuggestionList.Add(memberAddressDetails); 
      ViewBag.IsPrimaryCompany = primaryCompany; 
      ViewBag.IsPrimaryAddress = mainAddress; 
      return PartialView("_PopDivSuggestionAddress", addresssuggestionList); //Jquery dialog loads perfect 
     } 
     return RedirectToAction("Index");//here suppose fresh load index, not jQuery dialog. 
    } 
+0

我們在數據發送一個參數去檢查是否在對話框與否,然後根據負載,如果我們執行的成功方法重定向,它可能會解決問題。 – Dreamweaver

+0

任何示例,因爲我發送partialview或視圖。這些都是html數據。 – James123

+0

所以基本上對於要刷新同一頁面的返回視圖,正確嗎? – Dreamweaver

回答

0
function openPopup(data) 
{ 
    $("#dialogPopSuggestions").dialog({ 
      autoOpen: false, 
      height: 600, 
      width: 500, 
      modal: true, 
      title: "Suggesion Addresses", 
      //dialogClass: 'warning-dialog', 
      open: function (event, ui) { 
       //Load the action which will return 
       // the partial view 
       $(this).html(data); 
       event.preventDefault(); 
        event.stopPropagation(); 
      } 
     }); 
     $("#dialogPopSuggestions").dialog("open"); 
} 


$.ajax({ 
    url: '/Cntrllr/SaveAddress', 
    type: 'Post', 
    cache: false, 
    data: $("#frmAddress").serialize(), 
    success: function (data) { 
     if(data ==null){ 
      location.href='/Cntrllr/Index'; 
     } 
     else{ 
      openPopup(data); 
     } 

    }, 
    error: function (xhr, ajaxOptions, thrownError) { 
     alert("error occured"); 
     location.href='/Cntrllr/Index'; 
    } 

你的控制器將

[HttpPost] 
public ActionResult SaveAddress(FormCollection col) 
{ .... 
    .... 
    if (addresssuggestionList.Count == 0) //no suggestion 
    { 
     memberAddressDetails = memberAddress.AddAddress(memberId, memberAddressDetails); 
     return null; 
    } 
    else 
    { 
     addresssuggestionList.Add(memberAddressDetails); 
     ViewBag.IsPrimaryCompany = primaryCompany; 
     ViewBag.IsPrimaryAddress = mainAddress; 
     return PartialView("_PopDivSuggestionAddress", addresssuggestionList); //Jquery dialog loads perfect 
    }   
} 
相關問題