2013-02-28 56 views
0

這裏是我的視圖中的ASP MVC代碼。它觸發關閉控制器並將由assetid和relationshipContext:使用Ajax防止回傳的HTML.ActionLink到控制器

@Html.ActionLink("Archive", "Archive", new { assetId = Model.ID, relationshipContext = assetTypeRelation.RelationshipContextID }, new { @class = "btn btn-mini btn-warning", id = "btnArchive" }) 

我想利用Ajax和這個HTML.ActionLink,但我有點困惑。這裏是我開始使用的jQuery。基本上我只需要這個actionlink將assetId和relationshipContext傳遞給我的assetcontroller中的Archive方法。

$('#btnArchive').click(function(){ 
        $.ajax({ 
         url: '@Url.Action("Archive", "Archive")', 
         type: 'POST', 
         dataType: 'json', 
         data: { 
          assetId: $(this).attr("assetId"), 
          relationshipContext: $(this).attr("relationshipContext"), 
         }, 
         success: function(){ 
          alert("success") 
         }, 
         error: function() { 
          alert("error") 
         }, 

        }); 
+0

我沒有認出頂部的那種語言。我做了一次Google搜索,發現它是asp.net的一部分,特別是mvc的一部分。我添加了asp.net-mvc標籤,但是您可能想向代碼添加一些解釋。 – 2013-02-28 21:31:00

+0

你想混合服務器和客戶端代碼。 – gdoron 2013-02-28 21:33:06

+0

感謝您添加額外的標記,我更新了我的問題的正文。 – einsteinix 2013-02-28 21:36:16

回答

0

什麼是您的AssetController的操作方法是什麼樣子?我應該看起來像這樣:

[HttpPost] 
public ActionResult Archive(int assetId, string relationshipContext) 
{ 
    //do some work 
    return Content("true"); 
} 

此外,您在@ Url.Action調用中調用錯誤的控制器。您的查看代碼可能需要更改爲

$('#btnArchive').click(function(){ 
        $.ajax({ 
         url: '@Url.Action("Archive", "Asset")', 
         type: 'POST', 
         dataType: 'json', 
         data: { 
          assetId: $(this).attr("assetId"), 
          relationshipContext: $(this).attr("relationshipContext"), 
         }, 
         success: function(data){ 
          alert(data) 
         }, 
         error: function() { 
          alert("error") 
         }, 

        });