2013-02-28 61 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") 
         }, 

        });