2014-06-17 55 views
-2

我是新使用Ajax,我試圖調用從Ajax方法後動作像重定向使用Ajax POST

$(".buttonSelection").click(function() { 

    selectedId = $(this).parents('tr:first').children('td:first').children('input:first').attr('value'); 
    $.ajax({ 
     // Call MaSelection action method 
     url: "/DemandeLocation/MaSelectionOffre", 
     data: { id: selectedId }, 
     type: 'Post', 
     success: function (msg) { 
      window.location.replace('@Url.Content("~/DemandeLocation/MaSelectionOffre")'); 
     }, 
     error: function (xhr) { 
      alert("something seems wrong"); 
     } 
    }); 
    }); 

我的帖子方法去與成功,但不是redirectin我到MaSelection查看它返回我調用方法的第一個視圖,所以我試圖在我的ajax方法中放入一個「成功」片段,並將位置替換爲「Ma選擇」視圖,但我知道該視圖丟失了該ID,因此它變成了null,我怎麼能用Ajax做到這一點,

這裏我的貼子動作了解更多詳情

[HttpPost] 
    [Authorize(Roles = "Locataire")] 
    public ActionResult MaSelectionOffre(string id) 

    { 
     int DemandeLocationGetbyId = Convert.ToInt32(id); 

     var selectionOffre = db.SelectionOffreLocationSet.Where(model => model.DemandeLocationPublication_ID == DemandeLocationGetbyId).ToList(); 

     return View("MaSelectionOffre", selectionOffre); 
    } 
+0

難道你不能只通過selectedId與目標url你重定向到? –

回答

1

使用json作爲數據類型;

$(".buttonSelection").click(function() { 

    selectedId = $(this).parents('tr:first').children('td:first').children('input:first').attr('value'); 
    $.ajax({ 
     // Call MaSelection action method 
     url: "/DemandeLocation/MaSelectionOffre", 
     dataType:"json", 
     data: { id: selectedId }, 
     type: 'Post', 
     success: function (msg) { 
      window.location.href = msg.redirect; 
     }, 
     error: function (xhr) { 
      alert("something seems wrong"); 
     } 
    }); 
    }); 

你也需要這個;

Convert object to JSON string in C#

+0

它不是一個數據類型的問題,我的行動得到的ID和返回的項目列表,我試過這個,但它給了我一些東西似乎是錯誤的 – 404NotFound

1

如果你想重定向頁面,Ajax調用後,你應該使用

... 
success: function (msg) { 
    window.location.href = '@Url.Action("MaSelectionOffre", "DemandeLocation")'; 
}, 
... 

編輯

如果你想更換的結果,使用類似以下內容:

HTML

<div id="updateTargetId"> 
    //table 
     //tr 
      //td 
       //your button that has cssClass buttonSelection 
</div> 

JS

$(".buttonSelection").click(function() { 

    selectedId = $(this).parents('tr:first').children('td:first').children('input:first').attr('value'); 
    $.ajax({ 
     // Call MaSelection action method 
     url: "/DemandeLocation/MaSelectionOffre", 
     dataType:"json", 
     data: { id: selectedId }, 
     type: 'Post', 
     success: function (msg) { 
      $("#updateTargetId").html(msg); 
     }, 
     error: function (xhr) { 
      alert("something seems wrong"); 
     } 
    }); 
}); 

控制器(返回PartialView)

[HttpPost] 
[Authorize(Roles = "Locataire")] 
public ActionResult MaSelectionOffre(string id) 

{ 
    int DemandeLocationGetbyId = Convert.ToInt32(id); 

    var selectionOffre = db.SelectionOffreLocationSet.Where(model => model.DemandeLocationPublication_ID == DemandeLocationGetbyId).ToList(); 

    return PartialView("MaSelectionOffre", selectionOffre); 
} 
+0

這裏是場景,我打電話給MaselectionAction返回一個項目列表視圖MaSelection ,當我把retrun視圖與selectionOffre列表的工作,以及我想要的問題,但行動成功後,它返回到ajax和檢查sucess片段,當我把window.location.href ='@ Url.Action(「MaSelectionOffre」,「DemandeLocation」)';視圖丟失選擇列表並返回「無法找到資源」。 – 404NotFound

+0

@ 404NotFound,我更新了我的答案。 –

0

我改變了我的行動爲取得動作,在我的按鈕,我剛纔添加的窗口.location.replace with link and ID

<button type="button" class="buttonSelection" onclick="window.location.replace('@Url.Content("~/DemandeLocation/MaSelectionOffre?id="+item.Publication_ID)')"> <span class="ui-icon ui-icon-cart"></span> </button>